通过广播意图传递ArrayList

时间:2012-11-06 17:25:55

标签: android android-intent nullpointerexception broadcast parcelable

我跟着Help with passing ArrayList and parcelable Activity的例子,但是对于我的情况,我正试图从我的Service类(Inside Asynctask)广播一个intent到这样的Activity类。

ArrayList<Malware> addyExtras = new ArrayList <Malware>();

for (int i = 0; i < fetch.size(); i++) {
    addyExtras.add (fetch.get(i));
}
Intent d = new Intent();
d.setAction(COMPLETED_INTENT);
d.putExtra("CurrentProgress", "completed");
d.putParcelableArrayListExtra("MalwareArray", addyExtras);
sendBroadcast(d);

我如何收到广播意图

ArrayList<Malware> myList;
myList = getIntent().getParcelableArrayListExtra("MalwareArray");
Log.d("onReceive", "got myList");

for (int i = 0; i < myList.size(); i++) {
    Malware a = myList.get(i);
    Log.d("onReceive", "App Name:" + a.getApp());
    //tv1.setText(a.getName() + " is from " + a.getState());
}

尝试接收意图时会遇到nullpointerexception

11-07 01:21:51.315: E/AndroidRuntime(18769): FATAL EXCEPTION: main
11-07 01:21:51.315: E/AndroidRuntime(18769): java.lang.RuntimeException: Error receiving broadcast Intent { act=com.analyze.project.CompletedReceiver flg=0x10 (has extras) } in com.analyze.project.AnalyzeActivity$1@41889670
11-07 01:21:51.315: E/AndroidRuntime(18769):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:737)
11-07 01:21:51.315: E/AndroidRuntime(18769):    at android.os.Handler.handleCallback(Handler.java:605)
11-07 01:21:51.315: E/AndroidRuntime(18769):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-07 01:21:51.315: E/AndroidRuntime(18769):    at android.os.Looper.loop(Looper.java:137)
11-07 01:21:51.315: E/AndroidRuntime(18769):    at android.app.ActivityThread.main(ActivityThread.java:4513)
11-07 01:21:51.315: E/AndroidRuntime(18769):    at java.lang.reflect.Method.invokeNative(Native Method)
11-07 01:21:51.315: E/AndroidRuntime(18769):    at java.lang.reflect.Method.invoke(Method.java:511)
11-07 01:21:51.315: E/AndroidRuntime(18769):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:974)
11-07 01:21:51.315: E/AndroidRuntime(18769):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:741)
11-07 01:21:51.315: E/AndroidRuntime(18769):    at dalvik.system.NativeStart.main(Native Method)
11-07 01:21:51.315: E/AndroidRuntime(18769): Caused by: java.lang.NullPointerException
11-07 01:21:51.315: E/AndroidRuntime(18769):    at com.analyze.project.AnalyzeActivity$1.onReceive(AnalyzeActivity.java:197)
11-07 01:21:51.315: E/AndroidRuntime(18769):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:728)
11-07 01:21:51.315: E/AndroidRuntime(18769):    ... 9 more

1 个答案:

答案 0 :(得分:1)

从日志AnalyzeActivity.java:197,你有一个变量,它在第197行意外地为空。

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {

    Log.d("TAG", "onReceive1()");
    //setIntent(intent);

    ArrayList<Malware> myList;
    // use local var intent 
    myList = intent.getParcelableArrayListExtra("MalwareArray");
    Log.d("onReceive", "got myList");

    for (int i = 0; i < myList.size(); i++) {
        Malware a = myList.get(i);
        Log.d("onReceive", "App Name:" + a.getApp());
        //tv1.setText(a.getName() + " is from " + a.getState());
    }
}