0
我的代碼工作正常,如果我的應用程序擴展活動,但如果我改變FragmentActivity(只)logcat的返回一個致命異常...安卓:當我旋轉FragmentActivity(簡單的例子)致命異常
我欣賞任何幫助,我創建了一個簡單的例子(這也打破):
package com.example.changeorientation;
import android.os.Bundle;
import android.app.Activity;
import android.content.res.Configuration;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
public class MainActivity extends FragmentActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
onCreate(new Bundle());
}
}
和AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.changeorientation"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.changeorientation.MainActivity"
android:label="@string/app_name"
android:configChanges="locale|orientation|screenSize|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
,最後的logcat:
10-28 22:52:28.824: E/AndroidRuntime(2675): FATAL EXCEPTION: main
10-28 22:52:28.824: E/AndroidRuntime(2675): java.lang.IllegalStateException: Already attached
10-28 22:52:28.824: E/AndroidRuntime(2675): at android.support.v4.app.FragmentManagerImpl.attachActivity(FragmentManager.java:1867)
10-28 22:52:28.824: E/AndroidRuntime(2675): at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:198)
10-28 22:52:28.824: E/AndroidRuntime(2675): at com.example.changeorientation.MainActivity.onCreate(MainActivity.java:11)
10-28 22:52:28.824: E/AndroidRuntime(2675): at com.example.changeorientation.MainActivity.onConfigurationChanged(MainActivity.java:22)
10-28 22:52:28.824: E/AndroidRuntime(2675): at android.app.ActivityThread.performConfigurationChanged(ActivityThread.java:3924)
10-28 22:52:28.824: E/AndroidRuntime(2675): at android.app.ActivityThread.handleConfigurationChanged(ActivityThread.java:4017)
10-28 22:52:28.824: E/AndroidRuntime(2675): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2106)
10-28 22:52:28.824: E/AndroidRuntime(2675): at android.os.Handler.dispatchMessage(Handler.java:99)
10-28 22:52:28.824: E/AndroidRuntime(2675): at android.os.Looper.loop(Looper.java:123)
10-28 22:52:28.824: E/AndroidRuntime(2675): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-28 22:52:28.824: E/AndroidRuntime(2675): at java.lang.reflect.Method.invokeNative(Native Method)
10-28 22:52:28.824: E/AndroidRuntime(2675): at java.lang.reflect.Method.invoke(Method.java:521)
10-28 22:52:28.824: E/AndroidRuntime(2675): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-28 22:52:28.824: E/AndroidRuntime(2675): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-28 22:52:28.824: E/AndroidRuntime(2675): at dalvik.system.NativeStart.main(Native Method)
在此先感謝
但我想補充的android:configChanges =「區域|方向|屏幕尺寸| keyboardHidden」>,以避免...如果我刪除的onCreate則設備不要通過景觀版本來改變他的佈局(不要自己創建) – naxo
我在我的項目中檢查並且不更改橫向版本的肖像版本 – naxo
@naxo在閱讀您的評論後編輯我的答案。我以前使用過這種方法,請檢查它是否對您有所幫助。謝謝:) – saiful103a