I have display this
graph in fragment class and when I change the orientation app will crash. How can I change orientation and display image in landscape mode without crashing an app.
Asked
Active
Viewed 1,175 times
1
Waliul Islam Alif
- 5
- 1
- 2
user2273146
- 1,542
- 2
- 13
- 25
-
Can you share the error message you get when the app crashes? – Rachit Sep 21 '16 at 04:55
-
@Rachit the terminal provide this error Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference. – user2273146 Sep 21 '16 at 04:57
2 Answers
13
When the orientation change then the state of fragment will be changed. You need to save the state of your fragment by calling
setRetainInstance(true);
in onCreate() method.
You can also use onRestoreInstanceState() method which will store your savedInstanceState
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
onCreate(savedInstanceState);
}
The best solution is just add
android:configChanges="orientation|screenSize|keyboardHideen"
for your activity which holds the fragment in your manifest file.
Piyush
- 24,288
- 6
- 40
- 72
-
@user2273146 follow this... also One more solution make a xml in landscape and put in layout-land make sure ids are same to this layout..... – Arjun saini Sep 21 '16 at 05:04
1
During orientation changes, android destroys your activity. Maybe you set something and it became invalid during that change. You can read more about it here: https://developer.android.com/guide/topics/resources/runtime-changes.html
You said you want to be able to change the orientation. Just for completeness, you can fix the orientation to landscape or portrait, see: Android - disable landscape mode?
Community
- 1
- 1
Cubicle257
- 335
- 3
- 14