gpt4 book ai didi

android - 通过广播 Intent 传递 ArrayList

转载 作者:行者123 更新时间:2023-11-30 03:56:30 27 4
gpt4 key购买 nike

我按照 Help with passing ArrayList and parcelable Activity 中的示例进行操作但就我而言,我正在尝试将一个 Intent 从我的服务类(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);

我如何接收广播 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());
}

它在尝试接收 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

最佳答案

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

关于android - 通过广播 Intent 传递 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13256181/

27 4 0