2014-10-31 55 views
0

我面對空指針異常這一類..Android的廣播接收器發送空指針異常

代碼工作完美的第一次,但第二或第三時間後,它給了我錯誤...

請幫我解決我的問題

class MyReceiver extends BroadcastReceiver { 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      if (intent.getAction() != null 
        && intent.getAction().equals(MyActions.CONTACT_LIST_UPDATE)) { 
      /* Toast.makeText(getActivity(), "Contact Updated", 
       Toast.LENGTH_LONG).show();*/ 
       mFavList.clear(); 
       if (!dbAdapter.isOpen()) { 
        dbAdapter.openForRead(); 
       } 

       mFavList.addAll(dbAdapter.getAllUsers()); 
       mFavoriteListAdapter = new FavoriteListAdapter(
         LayoutInflater.from(getActivity()), mFavList, getActivity()); 
       lstchat.setAdapter(mFavoriteListAdapter); 
       // dbAdapter.close(); 
       mFavoriteListAdapter.notifyDataSetChanged(); 
       if (mFavList.size() == 0) { 
       // llLoading.setVisibility(View.VISIBLE); 
       // tvLoadingMessage.setText("No Contacts"); 
       // pBar.setVisibility(View.GONE); 
       } else { 
        //llLoading.setVisibility(View.GONE); 
       } 

       pastChatList.clear(); 

       pastChatList.addAll(dbAdapter.getPastChats()); 
       if (!dbAdapter.isOpen()) { 
        dbAdapter.openForRead(); 
       } 
       dbAdapter.addAllGroups(pastChatList); 

       dbAdapter.close(); 
       mPastChatAdapter.notifyDataSetChanged(); 

      } 
     } 
    } 

logcat的

10-31 12:58:00.661: E/AndroidRuntime(3144): FATAL EXCEPTION: main 
10-31 12:58:00.661: E/AndroidRuntime(3144): java.lang.NullPointerException 
10-31 12:58:00.661: E/AndroidRuntime(3144):  at android.view.LayoutInflater.from(LayoutInflater.java:211) 
10-31 12:58:00.661: E/AndroidRuntime(3144):  at com.addonwebsolution.yarg.activity.ChatFragment$MyReceiver.onReceive(ChatFragment.java:134) 
10-31 12:58:00.661: E/AndroidRuntime(3144):  at android.support.v4.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:297) 
10-31 12:58:00.661: E/AndroidRuntime(3144):  at android.support.v4.content.LocalBroadcastManager.access$000(LocalBroadcastManager.java:46) 
10-31 12:58:00.661: E/AndroidRuntime(3144):  at android.support.v4.content.LocalBroadcastManager$1.handleMessage(LocalBroadcastManager.java:116) 
10-31 12:58:00.661: E/AndroidRuntime(3144):  at android.os.Handler.dispatchMessage(Handler.java:99) 
10-31 12:58:00.661: E/AndroidRuntime(3144):  at android.os.Looper.loop(Looper.java:137) 
10-31 12:58:00.661: E/AndroidRuntime(3144):  at android.app.ActivityThread.main(ActivityThread.java:5214) 
10-31 12:58:00.661: E/AndroidRuntime(3144):  at java.lang.reflect.Method.invokeNative(Native Method) 
10-31 12:58:00.661: E/AndroidRuntime(3144):  at java.lang.reflect.Method.invoke(Method.java:525) 
10-31 12:58:00.661: E/AndroidRuntime(3144):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739) 
10-31 12:58:00.661: E/AndroidRuntime(3144):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555) 
10-31 12:58:00.661: E/AndroidRuntime(3144):  at dalvik.system.NativeStart.main(Native Method) 

的埃羅r正在進行

mFavoriteListAdapter = new FavoriteListAdapter(
        LayoutInflater.from(getActivity()), mFavList, getActivity()); 
+0

使用'context'而不是'getActivity()'。你的'Context'是空的。 – Piyush 2014-10-31 07:38:34

+0

我正在使用片段,所以我寫了getActivity() – Android 2014-10-31 07:52:29

+0

但是在'onRecieve()'方法中它有它自己的'Context'。所以試試看吧。另外檢查你是否已經初始化你的'mFavList'。 – Piyush 2014-10-31 07:54:50

回答

0

因爲它是從背景開始的。此時,getActivity()爲null,則應使用onReceive方法的「context」參數。