从Brodcast意图尝试getExtra时出现NullPointerException

时间:2018-06-17 14:18:27

标签: android android-intent

我尝试发送ArrayList Brodcasting并从AppWidgetProvider类接收它

这是发件人类的代码:

 Intent intent1 = new Intent(getApplicationContext(), BakingAppWidgetProvider.class);
        Bundle args1 = new Bundle();
        args1.putSerializable("INGREDIENTS",ingredients);
        intent1.putExtra("BUNDLE", args1);
        this.sendBroadcast(intent1);

这是来自methode onRecive中的reciver类的代码:

 @Override
    public void onReceive(final Context context, final Intent intent)
    {
        super.onReceive(context, intent);
            final Bundle bundle = intent.getBundleExtra("BUNDLE");
            parcelables =(ArrayList<Ingredient>) bundle.getSerializable("INGREDIENTS");
            ingredients1.addAll(parcelables);
            
        if (BakingAppWidgetProvider.WIDGET_UPDATE_ACTION.equals(intent.getAction()))
        {
            if (BuildConfig.DEBUG)
                Log.d(BakingAppWidgetProvider.TAG, "onReceive "
                        + BakingAppWidgetProvider.WIDGET_UPDATE_ACTION);
                final AppWidgetManager appWidgetManager = AppWidgetManager
                        .getInstance(context);
            final ComponentName thisAppWidget = new ComponentName(
                    context.getPackageName(), BakingAppWidgetProvider.class.getName());
            final int[] appWidgetIds = appWidgetManager
                    .getAppWidgetIds(thisAppWidget);
            onUpdate(context, appWidgetManager, appWidgetIds);
        }
    }

当我把断点放在这一行时:

 ingredients1.addAll(parcelables);

数据收到成功,但删除断点时出现此错误:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.android.advancebakingapp, PID: 5218
                  java.lang.RuntimeException: Unable to start receiver com.example.android.advancebakingapp.Widget.BakingAppWidgetProvider: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
                      at android.app.ActivityThread.handleReceiver(ActivityThread.java:3018)
                      at android.app.ActivityThread.-wrap18(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1544)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6077)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
                      at com.example.android.advancebakingapp.Widget.BakingAppWidgetProvider.onReceive(BakingAppWidgetProvider.java:44)

我该如何解决这个问题?

0 个答案:

没有答案