ANDROID 屏幕方向变化

发布于 2024-11-06 20:50:27 字数 2375 浏览 0 评论 0原文

如何在 Android 中正确处理屏幕方向更改

谁能帮我解决这个问题吗?

我总是收到此错误:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风筝在阴天搁浅。 2024-11-13 20:50:27

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

崩溃发生在 MainActivity 的第 80 行。

为了避免重新创建您的活动,请将其添加到您的清单文件中,

android:configChanges="keyboardHidden|orientation"

并将其添加到活动中

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

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);
}
堇色安年 2024-11-13 20:50:27

将其放在

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

AndroidManifest.xml 中的活动声明中,如下所示:

 <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>

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>
梨涡少年 2024-11-13 20:50:27

您应该发布代码,无论如何,堆栈跟踪会告诉您很多信息:

MainActivity.storePreferences(MainActivity.java:80) 处的 Java 空指针异常

调试/检查您的代码..

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..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文