我正在使用一个包含Activity和两个Fragments(android.support.v4.app.Fragment)的应用程序,但是我遇到了一个非常奇怪的问题。问题是当我旋转平板电脑时,应用程序会立即崩溃。
几天前,一切都正常运行,但我不得不更改软件包名称,这引发了一些错误,但我设法纠正了它们。问题出现在那之后,但我就是找不到错误所在的地方。以下是应用程序被销毁、重新启动、恢复和创建后获取的Logcat日志:
清单就在这里:
几天前,一切都正常运行,但我不得不更改软件包名称,这引发了一些错误,但我设法纠正了它们。问题出现在那之后,但我就是找不到错误所在的地方。以下是应用程序被销毁、重新启动、恢复和创建后获取的Logcat日志:
10-08 17:00:14.930: D/dalvikvm(18155): GC_CONCURRENT freed 394K, 8% free 7792K/8455K, paused 1ms+4ms
10-08 17:00:14.930: D/AndroidRuntime(18155): Shutting down VM
10-08 17:00:14.930: W/dalvikvm(18155): threadid=1: thread exiting with uncaught exception (group=0x40bee1f8)
10-08 17:00:14.938: E/AndroidRuntime(18155): FATAL EXCEPTION: main
10-08 17:00:14.938: E/AndroidRuntime(18155): java.lang.IllegalStateException: No activity
10-08 17:00:14.938: E/AndroidRuntime(18155): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1070)
10-08 17:00:14.938: E/AndroidRuntime(18155): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1065)
10-08 17:00:14.938: E/AndroidRuntime(18155): at android.support.v4.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:1854)
10-08 17:00:14.938: E/AndroidRuntime(18155): at android.support.v4.app.FragmentActivity.onResumeFragments(FragmentActivity.java:431)
10-08 17:00:14.938: E/AndroidRuntime(18155): at android.support.v4.app.FragmentActivity$1.handleMessage(FragmentActivity.java:90)
10-08 17:00:14.938: E/AndroidRuntime(18155): at android.os.Handler.dispatchMessage(Handler.java:99)
10-08 17:00:14.938: E/AndroidRuntime(18155): at android.os.Looper.loop(Looper.java:137)
10-08 17:00:14.938: E/AndroidRuntime(18155): at android.app.ActivityThread.main(ActivityThread.java:4514)
10-08 17:00:14.938: E/AndroidRuntime(18155): at java.lang.reflect.Method.invokeNative(Native Method)
10-08 17:00:14.938: E/AndroidRuntime(18155): at java.lang.reflect.Method.invoke(Method.java:511)
10-08 17:00:14.938: E/AndroidRuntime(18155): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
10-08 17:00:14.938: E/AndroidRuntime(18155): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
10-08 17:00:14.938: E/AndroidRuntime(18155): at dalvik.system.NativeStart.main(Native Method)
问题来自FragmentManager,但为什么我不知道,因为我没有任何细节。我尝试了在这个网站上找到的解决方案来获取更多信息,通过捕获未捕获的异常,但我得到的结果是相同的Logcat一遍又一遍,直到发生某种内存不足错误(logcat中有太多字符串)。
我的Activity布局如下,它在android生命周期中工作正常,但不适用于屏幕旋转:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id = "@+id/mainview"
tools:context=".MyActivity" >
<fragment
android:id="@+id/missionFragment"
android:layout_width="350dip"
android:layout_height="match_parent"
android:layout_marginTop="?android:attr/actionBarSize"
android:name="com.xxx.xxx.MissionFragment">
</fragment>
<fragment
android:id="@+id/mapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.xxx.xxx.MapFragment">
</fragment>
</LinearLayout>
清单就在这里:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxx.xxx"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="11" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:icon="@drawable/logo"
android:label="@string/app_name" >
<activity
android:name=".MySIGActivity"
android:label="@string/app_name"
android:windowSoftInputMode="stateHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
如果有人知道错误出现在哪个Android生命周期方法中,欢迎提供意见!
谢谢。
更新1.1: 这是我的主Activity的onCreate方法:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = getApplicationContext();
currentEtape = 1;
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
// Désactiver l'affichage du titre de l'application et son icône
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayShowTitleEnabled(true);
setContentView(R.layout.main);
SharedPreferences prefs = getPreferences(0);
trackingGPS = prefs.getBoolean("trackingGPS", false);
if(trackingGPS){
TextView viewTrackingActive = (TextView)findViewById(R.id.textview_notif_tracking);
viewTrackingActive.setText(this.getString(R.string.active));
viewTrackingActive.setTextColor(Color.parseColor("#99CC00"));
}
else{
TextView viewTrackingDesactive = (TextView)findViewById(R.id.textview_notif_tracking);
viewTrackingDesactive.setText(this.getString(R.string.desactive));
viewTrackingDesactive.setTextColor(Color.parseColor("#CC0000"));
}
// No interaction with Fragment or Activity classes in these methods
instanciateLayersDialog();
instanciateBasemapDialog();
}
关于 onResume 方法:
public void onResume(){
super.onResume();
SharedPreferences prefs = getPreferences(0);
trackingGPS = prefs.getBoolean("trackingGPS", false);
System.out.println("activity resume");
}