1

私はHelpの例に従ってArrayListとparcelableActivityを渡しましたが、私の場合は、Serviceクラス(Inside Asynctask)からこのような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
4

1 に答える 1

1

ログAnalyzeActivity.java:197から、197行目に予期せずnullである変数があります。

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());
    }
}
于 2012-11-06T17:39:00.713 に答える