从BroadcastReceiver中的服务获取arraylist会产生NullpointerException

时间:2016-10-15 06:59:53

标签: java android nullpointerexception

我在AsyncTask中有一个Service。我从ArrayList发送AsyncTask广播。

当我在ArrayList中获得onReceive()时,我得到NullpointerException

这是我发送ArrayList的方式。

 transits_list = new ArrayList<Transit>();
 transits_list.add(trs);

Intent arrayListIntent = new Intent("arrayList");
Bundle extra = new Bundle();
extra.putSerializable("transArray", transits_list);
intent.putExtra("extra", extra);
sendBroadcast(arrayListIntent);

Transit类实现Serializable

收到ArrayList

@Override
    public void onReceive(Context context, Intent intent) {
        ArrayList<Transit> myList;

        Bundle extra = getIntent().getBundleExtra("extra");
        ArrayList<Transit> transArrayListFromBroadCast =     (ArrayList<Transit>) extra.getSerializable("transArray");
        System.out.print("transArrayListFromBroadCast "+transArrayListFromBroadCast);
    }

我在这行中得到NullpointerException

ArrayList<Transit> transArrayListFromBroadCast =     (ArrayList<Transit>) extra.getSerializable("transArray");

日志中的例外:

  

致命的例外:主要                                                                          java.lang.RuntimeException:在com.prematix.tollsystem.avcc.AvccActivity$ArrayListReceiver@42003268中接收广播Intent {act = arrayList flg = 0x10}时出错                                                                              在android.app.LoadedApk $ ReceiverDispatcher $ Args.run(LoadedApk.java:798)                                                                              在android.os.Handler.handleCallback(Handler.java:800)                                                                              在android.os.Handler.dispatchMessage(Handler.java:100)                                                                              在android.os.Looper.loop(Looper.java:194)                                                                              在android.app.ActivityThread.main(ActivityThread.java:5391)                                                                              at java.lang.reflect.Method.invokeNative(Native Method)                                                                              在java.lang.reflect.Method.invoke(Method.java:525)                                                                              在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:833)                                                                              在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)                                                                              at dalvik.system.NativeStart.main(Native Method)                                                                           引起:java.lang.NullPointerException                                                                              at com.prematix.tollsystem.avcc.AvccActivity $ ArrayListReceiver.onReceive(AvccActivity.java:271)                                                                              在android.app.LoadedApk $ ReceiverDispatcher $ Args.run(LoadedApk.java:788)                                                                              在android.os.Handler.handleCallback(Handler.java:800)                                                                              在android.os.Handler.dispatchMessage(Handler.java:100)                                                                              在android.os.Looper.loop(Looper.java:194)                                                                              在android.app.ActivityThread.main(ActivityThread.java:5391)                                                                              at java.lang.reflect.Method.invokeNative(Native Method)                                                                              在java.lang.reflect.Method.invoke(Method.java:525)

1 个答案:

答案 0 :(得分:0)

获取Intent的方法不正确。我相信您的BroadcastReceiver位于活动类中,因为您正在调用getIntent()。但是,getIntent()会将Intent提供给活动,而不是接收者。 Intent的{​​{1}}已提供给方法receiver本身。对您的代码进行以下更改:

添加额外的:

onReceive()

获取额外费用:

Intent arrayListIntent = new Intent("arrayList");
Bundle extra = new Bundle();
extra.putSerializable("transArray", transits_list);
intent.putExtra("extra", extra);
sendBroadcast(arrayListIntent);