2011-05-12 237 views
0

如何正確處理屏幕方向更改在android中?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 
+0

您使用的是「服務」還是「光標」? – 2011-05-12 07:30:28

回答

0

你應該張貼的代碼,反正堆棧跟蹤告訴你很多:

的Java在MainActivity.storePreferences空指針異常(MainActivity.java: 80)

調試/檢查你的代碼..

4

at 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); 
} 
+0

我在我的佈局中有一個EditText視圖,我使用了setContentView(R.layout.myLayout),但是當屏幕方向改變時,我在EditText裏面輸入的字符被刪除了。 – CMA 2011-05-12 08:06:18

+0

抱歉我的天真問題..我完全新的Android ...我真的很感激你的幫助.. :) – CMA 2011-05-12 08:08:37

+1

使用[this](http://stackoverflow.com/questions/5657828/edittext-is-losing-users-input-when-rotating-android-phone/5657992#5657992) – Reno 2011-05-12 08:14:04

2

將這個

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

謝謝,這也幫助我很多我的應用程序。 – 2012-10-05 16:01:13