简体   繁体   中英

ANDROID screen orientation changes

How can I properly handle screen orientation changes in android?

Can anyone help me solve this problem?

I always get this error:

05-12 07:20:38.223: ERROR/AndroidRuntime(410): FATAL EXCEPTION: main
05-12 07:20:38.223: ERROR/AndroidRuntime(410): java.lang.RuntimeException: Unable to destroy activity {com.loginpage/com.loginpage.MainActivity}: java.lang.NullPointerException
05-12 07:20:38.223: ERROR/AndroidRuntime(410):     at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3655)
05-12 07:20:38.223: ERROR/AndroidRuntime(410):     at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3673)
05-12 07:20:38.223: ERROR/AndroidRuntime(410):     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3789)
05-12 07:20:38.223: ERROR/AndroidRuntime(410):     at android.app.ActivityThread.access$2400(ActivityThread.java:125)
05-12 07:20:38.223: ERROR/AndroidRuntime(410):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2037)
05-12 07:20:38.223: ERROR/AndroidRuntime(410):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-12 07:20:38.223: ERROR/AndroidRuntime(410):     at android.os.Looper.loop(Looper.java:123)
05-12 07:20:38.223: ERROR/AndroidRuntime(410):     at android.app.ActivityThread.main(ActivityThread.java:4627)
05-12 07:20:38.223: ERROR/AndroidRuntime(410):     at java.lang.reflect.Method.invokeNative(Native Method)
05-12 07:20:38.223: ERROR/AndroidRuntime(410):     at java.lang.reflect.Method.invoke(Method.java:521)
05-12 07:20:38.223: ERROR/AndroidRuntime(410):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-12 07:20:38.223: ERROR/AndroidRuntime(410):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-12 07:20:38.223: ERROR/AndroidRuntime(410):     at dalvik.system.NativeStart.main(Native Method)
05-12 07:20:38.223: ERROR/AndroidRuntime(410): Caused by: java.lang.NullPointerException
05-12 07:20:38.223: ERROR/AndroidRuntime(410):     at com.loginpage.MainActivity.storePreferences(MainActivity.java:80)
05-12 07:20:38.223: ERROR/AndroidRuntime(410):     at com.loginpage.MainActivity.onDestroy(MainActivity.java:29)
05-12 07:20:38.223: ERROR/AndroidRuntime(410):     at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3642)
05-12 07:20:38.223: ERROR/AndroidRuntime(410):     ... 12 more

at com.loginpage.MainActivity.storePreferences(MainActivity.java:80)

The crash occurred at line 80 of MainActivity.

To avoid recreation of your activity add this to your manifest file

android:configChanges="keyboardHidden|orientation"

and this to the activity

@Override
public void onConfigurationChanged(Configuration newConfig) {
  super.onConfigurationChanged(newConfig);
  setContentView(R.layout.myLayout);
}

Put this

 android:screenOrientation="sensor"
 android:configChanges="keyboardHidden|orientation"

on the declaration of the activity in the AndroidManifest.xml like this :

 <activity android:name=".activity.activityname"
                  android:label="@string/app_name"
                  android:theme="@android:style/Theme.Light"
                  android:windowSoftInputMode="adjustPan"
                  android:screenOrientation="sensor"
                  android:configChanges="keyboardHidden|orientation">
         </activity>

You should post the code, anyway, the stack trace tells you a lot:

Java null pointer exception at MainActivity.storePreferences(MainActivity.java:80)

Debug/check your code..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM