android广播接收器发送空指针异常

时间:2014-10-31 07:36:35

标签: android broadcastreceiver

我在这堂课中面临Null Pointer Exception ..

这段代码第一次完美无缺,但在第二次或第三次之后,它给了我错误......

请帮我解决我的问题

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)

错误发生

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

1 个答案:

答案 0 :(得分:0)

因为它是从背景开始的。这时,getActivity()为null,你应该使用onReceive方法的“ context ”参数。