I was trying to programmatically add to my Activity a list of Fragments that implement some CardViews. These CardViews have some TextViews that I would like to define from my Activity, and in fact it works by setting them in events after the Fragment's onCreateView, such as onStart and onResume. Or rather, no ... Better to say, it almost works. When there is a screen rotation (and everything that would force the restart of an Activity), trying to do something with any Fragment, even in the onStart and onRestart methods, results in a NullPointerException. Basically I have:
MyActivity.java
private List<MyFragment> cards = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rucardapio);
// Check that the activity is using the layout version with
// the fragment_container FrameLayout
if (findViewById(R.id.fragment_container) != null) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}
/* Here I create 10 fragment cards */
for(int i = 0; i < 10; i++) {
cards.add(new MyFragment());
transaction.add(R.id.fragment_container, cards.get(i));
}
transaction.commit();
}
}
After some tests, I assumed that the problem is in if (savedInstanceState != null) { return; }
. I tried these "solutions" that did not help:
link ,
link ,
link and more .. .
Logcat:
01-24 09:07:21.848 30751-30751/io.github.mths0x5f.guiaufu E/IMGSRV﹕ :0: PVRDRMOpen: TP3, ret = 43
01-24 09:07:21.878 30751-30751/io.github.mths0x5f.guiaufu E/IMGSRV﹕ :0: PVRDRMOpen: TP3, ret = 46
01-24 09:07:21.878 30751-30751/io.github.mths0x5f.guiaufu E/IMGSRV﹕ :0: PVRDRMOpen: TP3, ret = 47
01-24 09:07:21.878 30751-30751/io.github.mths0x5f.guiaufu E/IMGSRV﹕ :0: PVRDRMOpen: TP3, ret = 47
01-24 09:07:21.878 30751-30751/io.github.mths0x5f.guiaufu E/IMGSRV﹕ :0: PVRDRMOpen: TP3, ret = 47
01-24 09:07:21.888 30751-30751/io.github.mths0x5f.guiaufu E/IMGSRV﹕ :0: PVRDRMOpen: TP3, ret = 48
01-24 09:07:40.088 30751-30751/io.github.mths0x5f.guiaufu E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: io.github.mths0x5f.guiaufu, PID: 30751
java.lang.NullPointerException
at io.github.mths0x5f.guiaufu.ru.MyActivity$2.run(MyActivity.java:161)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5102)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:803)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:619)
at dalvik.system.NativeStart.main(Native Method)
Code that works at application startup but not after a configuration change:
@Override
protected void onStart() {
super.onStart();
((TextView) cards.get(0).getView().findViewById(R.id.textView)).setText("Test");
}