我正在调用方法AlertUser.alertUser(getActivity());
,它具有来自片段的alertDialog,但它会抛出NPE错误
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if(flag==false)
{
progressDialog.cancel();
AlertUser.alertUser(getActivity());
cancel(true);
}
}
}, 30000);
alertUser方法:
public static void alertUser(Context context)
{
AlertDialog.Builder alertDialog=new AlertDialog.Builder(context);
alertDialog.setTitle("Connection failure...!"+"\n");
alertDialog.setMessage("\n"+"It looks like you have lost network connection. Please check your network connection or wait to re-connect"+"\n");
// Setting "Yes" Button
alertDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
/*Intent noNetwork=new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
startActivityForResult(noNetwork,1443);*/
}
});
alertDialog.show();
}
logcat的
02-05 15:47:06.440: E/AndroidRuntime(12050): FATAL EXCEPTION: main
02-05 15:47:06.440: E/AndroidRuntime(12050): java.lang.NullPointerException
02-05 15:47:06.440: E/AndroidRuntime(12050): at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:142)
02-05 15:47:06.440: E/AndroidRuntime(12050): at android.app.AlertDialog$Builder.<init>(AlertDialog.java:359)
02-05 15:47:06.440: E/AndroidRuntime(12050): at com.abs.deliveryboy.AlertUser.alertUser(AlertUser.java:11)
02-05 15:47:06.440: E/AndroidRuntime(12050): at com.abs.deliveryboy.NewOrdersFragment$NewOrderTask$1.run(NewOrdersFragment.java:159)
02-05 15:47:06.440: E/AndroidRuntime(12050): at android.os.Handler.handleCallback(Handler.java:615)
02-05 15:47:06.440: E/AndroidRuntime(12050): at android.os.Handler.dispatchMessage(Handler.java:92)
02-05 15:47:06.440: E/AndroidRuntime(12050): at android.os.Looper.loop(Looper.java:137)
02-05 15:47:06.440: E/AndroidRuntime(12050): at android.app.ActivityThread.main(ActivityThread.java:4800)
02-05 15:47:06.440: E/AndroidRuntime(12050): at java.lang.reflect.Method.invokeNative(Native Method)
02-05 15:47:06.440: E/AndroidRuntime(12050): at java.lang.reflect.Method.invoke(Method.java:511)
02-05 15:47:06.440: E/AndroidRuntime(12050): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
02-05 15:47:06.440: E/AndroidRuntime(12050): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
02-05 15:47:06.440: E/AndroidRuntime(12050): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
您传递给AlertDialog.Builder
构造函数的上下文是null
,因此是NPE。
如果片段未附加到活动,片段getActivity()
将返回null
。由于您将异步runnable发布到处理程序,因此无法保证在runnable运行时将该片段附加到活动。
您应该将有效的活动上下文作为参数传递给您使用getActivity()
查询它的runnable。请注意,保留活动引用是一种容易导致内存泄漏的方法。
答案 1 :(得分:0)
正如@FD_所说,你无法从后台线程启动警告对话框。在后台线程中使用getActivity()
时,它返回null,因此返回NPE。您正在将null对象传递给另一个方法。最好在同一个活动/线程上做。
答案 2 :(得分:0)
试试这个..
public static void alertUser(Context context)
{
AlertDialog.Builder alertDialog=new AlertDialog.Builder(context);
alertDialog.setTitle("Connection failure...!"+"\n");
alertDialog.setMessage("\n"+"It looks like you have lost network connection. Please check your network connection or wait to re-connect"+"\n");
// Setting "Yes" Button
alertDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
/*Intent noNetwork=new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
startActivityForResult(noNetwork,1443);*/
}
});
//previously it was like this alertDialog.show(); but it should be like this
alertDialog.create.show();
}