I have to update a listview in a fragment if a file succesfully downloads. So in my download manager's BroadcastReceiver I register this new broadcast receiver:
Intent intent = new Intent();
intent.setAction("CONTENTS_NOTIFICATION");
context.sendBroadcast(intent);
And in my fragment in onCreateView I add the following code in order to register the receiver:
IntentFilter filter = new IntentFilter("CONTENTS_NOTIFICATION");
getActivity().getApplicationContext().registerReceiver(myReceiver, filter);
then:
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);
}
};
and at the end:
public void onDestroyView() {
super.onDestroyView();
getActivity().getApplicationContext().unregisterReceiver(myReceiver);
}
I have to add two of these broadcast receivers for two different Fragment. On the first one, all works fine. If in the application the user is on the fragment, it updates, while on the second fragment I got these errors:
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
and the code is the same! I can't get why it's not working. It says there is a problem with populatelistView but the same function works fine inside the fragment if not called from the broadcast receiver.
The only difference I can think between this two fragments is that the one in which the update works is the first one opened when the application is launched.