在我的代码中,当互联网不可用时,我显示了一个警告对话框。在该对话框中,我设置了一条消息" Internet Not Available。"在那个对话框中,我放了两个按钮'刷新' &安培; '设置&#39 ;.当互联网消失了&对话框来了,我点击了它给出的设置按钮
这是错误(Logcat)。
E: FATAL EXCEPTION: main Process:
com.appiphy.spacecomp.spinnerframework1, PID: 30424
java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread
android.app.ActivityThread.getApplicationThread()' on a null object
reference
at android.app.Activity.startActivityForResult(Activity.java:3823)
at android.app.Activity.startActivityForResult(Activity.java:3784)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:784)
at android.app.Activity.startActivity(Activity.java:4094)
at android.app.Activity.startActivity(Activity.java:4062)
at com.appiphy.spacecomp.spinnerframework1.activity.MainActivity$10.onClick(MainActivity.java:967)
at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:157)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5297)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));
我使用上面的行在设备上打开设置。
如何解决上述错误?
答案 0 :(得分:0)
试试这个:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(this, android.R.style.Theme_Holo_Light_Dialog));
alertDialogBuilder
.setMessage("No internet connec" +
"" +
"tion on your device. Would you like to enable it?")
.setTitle("No Internet Connection")
.setCancelable(false)
.setPositiveButton(" Enable Internet ",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent dialogIntent = new Intent(android.provider.Settings.ACTION_SETTINGS);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(dialogIntent);
}
});
alertDialogBuilder.setNegativeButton(" Cancel ", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
答案 1 :(得分:0)
我不知道您的对话框是在您的活动内还是在其他实用程序类中。如果它在活动之外,那么您应该使用上下文来启动意图。您可以传递活动的上下文,然后调用 Intent intent = new Intent(android.provider.Settings.ACTION_SETTINGS)); mContext.startActivity(意向); mContext是您将为活动传递的上下文。