我想從一個非活動類中調用我的Activity中的一個方法。但是app的關閉。無法弄清楚如何解決這個問題,所以我發佈了它。如何從非活動類調用靜態方法?
公共類MainActivity延伸AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NSOMUHUpdateChecker.checkForUpdates(getBaseContext());
}
public static void boob() {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DgrialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
公共類NSOMUHUpdateChecker {
public static void checkForUpdates(Context context) {
MainActivity.boob();
}
日誌:
FATAL EXCEPTION: main Process: nif.nhst, PID: 16378
java.lang.RuntimeException: Unable to start activity ComponentInfo{nif.nhst/nif.nhst.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
你在哪裏調用此方法? –
我從main中調用一個方法,在類中調用main中的方法。 Main-> Class-> Main – user8091716
什麼是方法和你叫 –