5

我的应用程序有一个非常奇怪的问题,它由一个 Activity 和 2 个片段(android.support.v4.app.Fragment)组成。

问题是当我旋转我的平板电脑时:应用程序立即崩溃。

几天前,一切正常,但我不得不更改包名称,这引发了一些错误,但我设法纠正了它们。在那之后问题出现了,但我就是找不到错误在哪里。这是我在应用程序被销毁、重新启动、恢复和创建后立即得到的 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: 这是我的主要活动的 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");
}
4

2 回答 2

13

I'm not sure if this answers the problem of the original poster, but I had this same problem when I had an onPause() method in my main Activity that looked like this:

@Override
public void onPause() {
    super.onResume();

    ...do other stuff...
}

As you can see, it was just a typo, but it took me a while to track it down since the error message "No Activity" was so vague. ("super.onResume" should have been "super.onPause", if it's not obvious.)

If this is not your exact issue, I believe it had something to do with the original Fragment not getting destroyed (by the proper onPause() call in my case).

于 2012-11-17T22:13:31.523 回答
-6

我发现了问题:android.support.v4.app 包是问题所在。我用来自 android.app 包的标准片段导入替换了所有导入。

使用这个包我对 Android 2.0 分支的兼容性现在被破坏了,但在我的情况下这不是问题。

于 2012-10-10T12:11:51.773 回答