2
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 set from my Activity, and in fact, it works by defining them in events after Fragment onCreateView, such as onStart and onResume. Or rather, no... it’s better to say, it almost works. When there is a screen rotation (and all that would force the restart of an Activity), trying to do something with any Fragment, even in 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 a few tests, I assumed the problem was in if (savedInstanceState != null) { return; }
. I tried those "solutions" that didn’t help me:
https://stackoverflow.com/questions/26389938/nullpointerexception-when-accessing-a-fragments-textview-in-an-activity ,
https://stackoverflow.com/questions/27748277/npe-thrown-when-trying-to-find-button-id-in-android-fragment ,
https://stackoverflow.com/questions/18534804/how-would-i-implement-a-generic-arraylist-of-fragments-in-android and much 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 on 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");
}
This is a correct answer and @Matheus WITHDREW AS CORRECT for futile reasons.
– Lollipop