如果文件成功下载,我必须更新片段中的列表视图。因此,在我的下载管理器的 BroadcastReceiver 中,我注册了这个新的广播接收器:
Intent intent = new Intent();
intent.setAction("CONTENTS_NOTIFICATION");
context.sendBroadcast(intent);
在我的 onCreateView 片段中,我添加了以下代码以注册接收器:
IntentFilter filter = new IntentFilter("CONTENTS_NOTIFICATION");
getActivity().getApplicationContext().registerReceiver(myReceiver, filter);
然后:
private BroadcastReceiver myReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
SQLiteDatabase db = DatabaseManager.getInstance(getActivity()).getWritableDatabase();
c = SelectedExperience.getSelectedExperiences(db);
String id_esperienza = "Selected Experience";
if (c.getCount() != 0) {
id_esperienza = c.getString(c.getColumnIndex(SelectedExperience.ID_ESPERIENZA));
}
populateListview(v, id_esperienza);
}
};
最后:
public void onDestroyView() {
super.onDestroyView();
getActivity().getApplicationContext().unregisterReceiver(myReceiver);
}
我必须为两个不同的片段添加两个广播接收器。在第一个上,一切正常。如果在应用程序中用户在片段上,它会更新,而在第二个片段上我收到以下错误:
03-15 07:32:40.474: E/AndroidRuntime(1692): FATAL EXCEPTION: main
03-15 07:32:40.474: E/AndroidRuntime(1692): java.lang.RuntimeException: Error receiving broadcast Intent { act=CONTENTS_NOTIFICATION flg=0x10 } in sample.actionscontentview.fragment.ContentsFragment$1@418e0850
03-15 07:32:40.474: E/AndroidRuntime(1692): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:768)
03-15 07:32:40.474: E/AndroidRuntime(1692): at android.os.Handler.handleCallback(Handler.java:725)
03-15 07:32:40.474: E/AndroidRuntime(1692): at android.os.Handler.dispatchMessage(Handler.java:92)
03-15 07:32:40.474: E/AndroidRuntime(1692): at android.os.Looper.loop(Looper.java:137)
03-15 07:32:40.474: E/AndroidRuntime(1692): at android.app.ActivityThread.main(ActivityThread.java:5191)
03-15 07:32:40.474: E/AndroidRuntime(1692): at java.lang.reflect.Method.invokeNative(Native Method)
03-15 07:32:40.474: E/AndroidRuntime(1692): at java.lang.reflect.Method.invoke(Method.java:511)
03-15 07:32:40.474: E/AndroidRuntime(1692): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
03-15 07:32:40.474: E/AndroidRuntime(1692): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
03-15 07:32:40.474: E/AndroidRuntime(1692): at dalvik.system.NativeStart.main(Native Method)
03-15 07:32:40.474: E/AndroidRuntime(1692): Caused by: java.lang.NullPointerException
03-15 07:32:40.474: E/AndroidRuntime(1692): at sample.actionscontentview.fragment.ContentsFragment.populateListview(ContentsFragment.java:194)
03-15 07:32:40.474: E/AndroidRuntime(1692): at sample.actionscontentview.fragment.ContentsFragment.access$0(ContentsFragment.java:111)
03-15 07:32:40.474: E/AndroidRuntime(1692): at sample.actionscontentview.fragment.ContentsFragment$1.onReceive(ContentsFragment.java:107)
03-15 07:32:40.474: E/AndroidRuntime(1692): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:758)
03-15 07:32:40.474: E/AndroidRuntime(1692): ... 9 more
和代码是一样的!我不明白为什么它不起作用。它说 populatelistView 存在问题,但如果不从广播接收器调用,相同的函数在片段内可以正常工作。
我认为这两个片段之间的唯一区别是,更新工作的片段是应用程序启动时打开的第一个片段。