1

I followed the example at Help with passing ArrayList and parcelable Activity but for my case i'm trying to broadcast an intent from my Service class (Inside Asynctask) to Activity class like this.

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);

How i receive the broadcast intent

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());
}

It hits a nullpointerexception when trying to receive the intent

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
Community
  • 1
  • 1
dythe
  • 840
  • 5
  • 21
  • 45

1 Answers1

1

from the log, AnalyzeActivity.java:197, you have a variable that is, unexpectedly, null on line 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());
    }
}
petey
  • 16,914
  • 6
  • 65
  • 97
  • It's a inner broadcast receiver class that i'm using in `AnalyzeActivity` to receive the broadcast from my Service don't think i can do it this way? – dythe Nov 06 '12 at 17:42
  • I edited my answer to use the local broadcast receiver from the support package. Give that a try. – petey Nov 06 '12 at 17:52
  • It's working now but the problem with my code is im trying to also pass a drawable over too. Do i have to convert my drawable to bytes to pass it over? – dythe Nov 06 '12 at 17:57
  • yea, if its not something your can pick up by 'getResources().getDrawable()' then covert that to bytes. it might be slow if its big tho – petey Nov 06 '12 at 18:02
  • Anytime, calling setintent will set the activity's intent. You may not want this in some cases if you use getIntent() a good deal elsewhere in your code. I changed my answer slightly to account for this. – petey Nov 06 '12 at 18:06