我想从一个非活动类中调用我的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
什么是方法和你叫 –