Tag: android context

从另一个类调用TextToSpeech

我试图在另一个类中调用TextToSpeech。 以下是我的课程现在的样子: //MainActivity.java public class MainActivity extends AppCompatActivity implements View.OnClickListener { private SpeechRecognizer sr; sr.setRecognitionListener(new Listener()); } //Listener.java public class Listener implements RecognitionListener() { public void onResults(Bundle MainActivity theMainActivity = new MainActivity(); //the following line always breaks the code: tts = new TextToSpeech(theMainActivity, new TextToSpeech.OnInitListener() {/*…*/}); } } 对于上下文,其上下文中的文件位于GitHub上 。 GitHub上的版本是MainActivity.java中所有内容的工作版本,但我试图将Listener移动到它自己的类Listener.java 。 收到的错误是java.lang.NullPointerException: Attempt to […]

Android内部类内存泄漏和上下文泄漏?

我在启动画面中使用Handler将重定向延迟到下一个活动,如下所示。 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.entrance); screenTimeOut(); } private void screenTimeOut() { /* New Handler to start the next screen * and close this Entrance after some seconds.*/ new Handler().postDelayed(new Runnable() { @Override public void run() { initTracker(); /* Create an Intent that will start the Next-Activity. */ checkLoginStatus(); } }, SPLASH_DISPLAY_LENGTH); […]

无法从AsyncTask启动WebView

我想从我的AsyncTask启动一个WebView但它似乎没有运行。 这是我的onPostExecute方法的样子: public class Viewer extends AsyncTask { private Activity objContext = null; public Viewer(Activity objContext) { this.objContext = objContext; } protected void onPostExecute(URI uriWebpage) { WebView wbvBrowser = new WebView(this.objContext); wbvBrowser.getSettings().setBuiltInZoomControls(true); wbvBrowser.getSettings().setJavaScriptEnabled(true); wbvBrowser.loadUrl(uriWebpage.toString()); } } 我的任务由我的应用程序中的两个活动使用,因此我的全局objContext变量的类型为Activity 。 如果我将objContext变量的类型更改为调用类的名称,它可以正常工作但我无法从其他调用类实例化我的任务。 我像这样实例化我的任务。 Viewer mytask = new Viewer(this); 我怎么解决这个问题?

如何在oncreate之外使用SharedPreferences?

如何在没有oncreate的类中使用SharedPreferences ? 访问它时我得到空指针。 public class Ftr extends Activity { SharedPreferences preferences; Context ab=this; public void ft() { preferences = PreferenceManager.getDefaultSharedPreferences(ab); String result = preferences.getString(“F”,””); } } 我从另一个活动调用函数ft() Ftr只是一个类而不是一个活动。 如何在这种情况下使用SharedPreferences ?

如何从android中的静态方法调用非静态方法

我在从静态方法调用非静态方法时遇到了一个大问题。 这是我的代码 Class SMS { public static void First_function() { SMS sms = new SMS(); sms.Second_function(); } public void Second_function() { Toast.makeText(getApplicationContext(),”Hello”,1).show(); // This i anable to display and cause crash CallingCustomBaseAdapters(); //this was the adapter class and i anable to call this also } 我能够调用Second_function但无法获取Toast和CallCustomBaseAdapter()方法,发生崩溃。 我该怎么做才能解决这个问题?

Android:我应该维护对recyclerview内部活动的引用,还是有另一种方式?

我需要在onBindViewHolder中使用上下文方法(标准示例可能像getString或getColor一样常见)。 到目前为止,我已经将上下文传递给了recyclerview的构造函数,并在变量中维护了一个引用,但在我看来,这似乎是不好的做法。 有没有办法从recyclelerview内部动态获取上下文而不将其存储为变量? public SomeRecyclerViewClass(Activity activity) { this.parentActivity = activity; }

使用android.content.Context.checkPermission检查权限时出现空指针exception

在查询Android日历以查找事件之前,我需要检查权限。 要做到这一点,Android工作室警告我需要在查询之前进行检查。 自动生成的代码就是这个: if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) { System.out.println(“NO ACCESS TO CALENDAR!! Abort mission, abort mission!!”); } 尝试运行它时,我收到此错误: 尝试调用虚方法’int null对象引用上的android.content.Context.checkPermission(java.lang.String,int,int)’ 所以很明显,此时某些东西是空的,我试图以不同的方式获取应用程序的上下文,但它仍然是相同的错误。 我试过的其他事情是这个代码,它应该处理低于Android 6的目标: if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CALENDAR}, MY_PERMISSIONS_REQUEST_READ_CONTACTS); } 仍然得到同样的错误,任何人都可以帮助我吗?

无法使用getApplication方法强制转换为Application

我有类App包含我的Application上下文。 但是当我编译时,我在这一行的其他类中遇到了error : App app = (App) getApplication(); class级App : import android.app.Application; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.media.MediaPlayer; import android.support.v4.app.NotificationCompat; import android.util.Log; import com.example.Radio_KPI.R; import com.example.Radio_KPI.Utils.Const; public class App extends Application { private MediaPlayer player; private NotificationManager manager; private String curSong; private Context Main_con; public Context getMain_con() { return […]