2014-02-05 47 views
0

我打電話方法AlertUser.alertUser(getActivity());具有alertDialog從片段,但它拋出NPE錯誤NPE同時創造alerdialog

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

AlertUser.java的第11行是什麼? – Michael

回答

0

由於@FD_說,你不能從後臺線程啓動一個警告對話框。當您在後臺線程中使用getActivity()時,它將返回null,因此將返回NPE。您將一個空對象傳遞給另一個方法。最好在同一個活動/線程上進行。

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(); 
    } 
+0

你應該編輯和解釋更好的答案 – joaquin

2

你傳遞給AlertDialog.Builder構造的背景是null,因此NPE。

片段getActivity()將返回null如果片段未附加到活動。由於您正在向處理程序發佈異步可運行內容,因此無法保證片段在運行時運行時會附加到某個活動。

您應該將一個有效的活動上下文作爲參數傳遞給您的可運行內存,並用getActivity()查詢它。請注意,保持活動引用是獲取重大內存泄漏的簡單方法。