应用程序在第二次重新启动时崩溃

时间:2012-08-21 14:44:01

标签: android exception android-fragments illegalstateexception

我目前正在将一个Iphone应用程序移植到Android上,因为我不知道我会用它们尝试它的片段,所以我创建了几个类(每个视图一个呈现给用户,所有扩展基类片段)和相应的xml布局。下面的代码显示启动时显示的片段。我为每个片段使用单例。

public class FragWelcome extends Fragment {

private static FragWelcome _fs;

OnClickListener mListener;

public FragWelcome() {}

public static FragWelcome getFrag() {
    if(FragWelcome._fs==null) _fs=new FragWelcome();
    return _fs;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        mListener = (OnClickListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString() + " must implement OnClickListener");
    }
}

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  View v = inflater.inflate(R.layout.frag_welcome, container, false);
  ((Button)v.findViewById(R.id.btnCreateNewList)).setOnClickListener(mListener);
  ((Button)v.findViewById(R.id.btnEditOldList)).setOnClickListener(mListener);
  return v;
}
}

然后,我的片段管理器类包含以下方法:

    public void addFragment(int container, Fragment f,int fadein,int fadeout, boolean addtobackstack,String tag) {

        this._currentFragment=f;
        this._fragTrans=this._fragMan.beginTransaction();
        this._fragTrans.setCustomAnimations(fadein, fadeout);
        this._fragTrans.add(container, f ,tag);
        if(addtobackstack) this._fragTrans.addToBackStack(tag);
        this._fragTrans.commit();

}
public void replaceFragment(int container, Fragment f,int fadein,int fadeout, boolean addtobackstack,String tag) {

        this._currentFragment=f;
        this._fragTrans=this._fragMan.beginTransaction();
        this._fragTrans.setCustomAnimations(fadein, fadeout);
        this._fragTrans.replace(container, f ,tag);
        if(addtobackstack) this._fragTrans.addToBackStack(tag);
        this._fragTrans.commit();

}

这是我的主要活动类AC_Main的onCreate方法。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ac_main);
    this.setTitle("Willkommen");

    FragMan.getSharedFragMan(this).addFragment(R.id.llMain,FragWelcome.getFrag(),R.animator.fade_in,R.animator.fade_out,false,"frag_welcome");  

}

我能说什么......一切都按预期工作,非常好。没有任何错误...只要我不尝试第二次启动应用程序。

08-21 16:27:18.278: D/AndroidRuntime(21576): Shutting down VM
08-21 16:27:18.278: W/dalvikvm(21576): threadid=1: thread exiting with uncaught exception (group=0x40c351f8)
08-21 16:27:18.283: E/AndroidRuntime(21576): FATAL EXCEPTION: main
08-21 16:27:18.283: E/AndroidRuntime(21576): java.lang.RuntimeException: Unable to start     activity ComponentInfo{de.lochmann.einkaufsliste/de.lochmann.einkaufsliste.AC_Main}: j     java.lang.IllegalStateException: Activity has been destroyed
08-21 16:27:18.283: E/AndroidRuntime(21576):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
08-21 16:27:18.283: E/AndroidRuntime(21576):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
08-21 16:27:18.283: E/AndroidRuntime(21576):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
08-21 16:27:18.283: E/AndroidRuntime(21576):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
08-21 16:27:18.283: E/AndroidRuntime(21576):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-21 16:27:18.283: E/AndroidRuntime(21576):    at android.os.Looper.loop(Looper.java:137)
08-21 16:27:18.283: E/AndroidRuntime(21576):    at android.app.ActivityThread.main(ActivityThread.java:4507)
08-21 16:27:18.283: E/AndroidRuntime(21576):    at java.lang.reflect.Method.invokeNative(Native Method)
08-21 16:27:18.283: E/AndroidRuntime(21576):    at java.lang.reflect.Method.invoke(Method.java:511)
08-21 16:27:18.283: E/AndroidRuntime(21576):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
08-21 16:27:18.283: E/AndroidRuntime(21576):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
08-21 16:27:18.283: E/AndroidRuntime(21576):    at dalvik.system.NativeStart.main(Native Method)
08-21 16:27:18.283: E/AndroidRuntime(21576): Caused by: java.lang.IllegalStateException: Activity has been destroyed
08-21 16:27:18.283: E/AndroidRuntime(21576):    at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1280)
08-21 16:27:18.283: E/AndroidRuntime(21576):    at android.app.BackStackRecord.commitInternal(BackStackRecord.java:541)
08-21 16:27:18.283: E/AndroidRuntime(21576):    at android.app.BackStackRecord.commit(BackStackRecord.java:525)
08-21 16:27:18.283: E/AndroidRuntime(21576):    at de.lochmann.einkaufsliste.FragMan.addFragment(FragMan.java:41)
08-21 16:27:18.283: E/AndroidRuntime(21576):    at de.lochmann.einkaufsliste.AC_Main.onCreate(AC_Main.java:124)
08-21 16:27:18.283: E/AndroidRuntime(21576):    at android.app.Activity.performCreate(Activity.java:4465)
08-21 16:27:18.283: E/AndroidRuntime(21576):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
08-21 16:27:18.283: E/AndroidRuntime(21576):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
08-21 16:27:18.283: E/AndroidRuntime(21576):    ... 11 more

所以,当我从Eclipse启动应用程序时,一切正常,应用程序完全稳定,直到我按下我的Android设备的主页按钮(三星Galaxy S2),然后我回到我的应用程序。当我再次尝试启动应用程序时,它会因错误而崩溃。任务经理说它不再在后台运行了。 所以我的问题是......我做错了什么,任何人都有自己的想法或经验?非常感谢。

我刚刚发现如果第二次我的应用程序崩溃并且我的设备告诉我它停止了应用程序,我可以在第三次尝试时再次正常运行应用程序。也许它仍处于背景中,碎片会造成麻烦......!

在我实现了每个生命周期方法之后编辑,新的LogCat输出,并且我调用了所有超级方法。

08-21 17:17:09.043: I/dalvikvm(25725): Turning on JNI app bug workarounds for target SDK version 10...
08-21 17:17:09.108: I/Activity(25725): onCreate called
08-21 17:17:09.108: I/FragWelcome(25725): onAttach called
08-21 17:17:09.108: I/FragWelcome(25725): onCreate called
08-21 17:17:09.123: D/dalvikvm(25725): GC_FOR_ALLOC freed 86K, 3% free 8909K/9091K, paused 13ms
08-21 17:17:09.128: I/dalvikvm-heap(25725): Grow heap (frag case) to 11.061MB for 2432016-byte allocation
08-21 17:17:09.148: D/dalvikvm(25725): GC_CONCURRENT freed 1K, 3% free 11282K/11527K, paused 1ms+1ms
08-21 17:17:09.203: D/dalvikvm(25725): GC_FOR_ALLOC freed 0K, 3% free 11283K/11527K, paused 11ms
08-21 17:17:09.208: I/dalvikvm-heap(25725): Grow heap (frag case) to 16.278MB for 5472016-byte allocation
08-21 17:17:09.228: D/dalvikvm(25725): GC_CONCURRENT freed 0K, 2% free 16626K/16903K, paused 1ms+2ms
08-21 17:17:09.318: I/FragWelcome(25725): onCreateView called
08-21 17:17:09.318: I/FragWelcome(25725): onActivityCreated called
08-21 17:17:09.323: I/Activity(25725): onStart called
08-21 17:17:09.323: I/FragWelcome(25725): onStart called
08-21 17:17:09.323: I/Activity(25725): onResume called
08-21 17:17:09.323: I/FragWelcome(25725): onResume called
08-21 17:17:09.503: D/CLIPBOARD(25725): Hide Clipboard dialog at Starting input: finished by someone else... !
08-21 17:17:21.258: I/FragWelcome(25725): onPause called
08-21 17:17:21.258: I/Activity(25725): onPause called
08-21 17:17:21.438: D/CLIPBOARD(25725): Hide Clipboard dialog at Starting input: finished by someone else... !
08-21 17:17:21.908: I/FragWelcome(25725): onStop called
08-21 17:17:21.908: I/Activity(25725): onStop called
08-21 17:17:21.908: I/FragWelcome(25725): onDestroyView called
08-21 17:17:21.908: I/FragWelcome(25725): onDestroy called
08-21 17:17:21.913: I/FragWelcome(25725): onDetach called
08-21 17:17:21.913: I/Activity(25725): onDestroy called
08-21 17:17:32.833: I/Activity(25725): onCreate called
08-21 17:17:32.833: D/AndroidRuntime(25725): Shutting down VM
08-21 17:17:32.833: W/dalvikvm(25725): threadid=1: thread exiting with uncaught exception (group=0x40c351f8)
08-21 17:17:32.838: E/AndroidRuntime(25725): FATAL EXCEPTION: main
08-21 17:17:32.838: E/AndroidRuntime(25725): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.lochmann.einkaufsliste/de.lochmann.einkaufsliste.AC_Main}: java.lang.IllegalStateException: Activity has been destroyed
08-21 17:17:32.838: E/AndroidRuntime(25725):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
08-21 17:17:32.838: E/AndroidRuntime(25725):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
08-21 17:17:32.838: E/AndroidRuntime(25725):    at android.app.ActivityThread.access$600(ActivityThread.java:127)

我从前logcat中读到的内容是backstackrecord.com的一些问题...有谁知道这件事?

08-21 16:27:18.283: E/AndroidRuntime(21576): Caused by: java.lang.IllegalStateException: Activity has been destroyed
08-21 16:27:18.283: E/AndroidRuntime(21576):    at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1280)
08-21 16:27:18.283: E/AndroidRuntime(21576):    at android.app.BackStackRecord.commitInternal(BackStackRecord.java:541)
08-21 16:27:18.283: E/AndroidRuntime(21576):    at android.app.BackStackRecord.commit(BackStackRecord.java:525)
08-21 16:27:18.283: E/AndroidRuntime(21576):    at de.lochmann.einkaufsliste.FragMan.addFragment(FragMan.java:41)
08-21 16:27:18.283: E/AndroidRuntime(21576):    at de.lochmann.einkaufsliste.AC_Main.onCreate(AC_Main.java:124)

3 个答案:

答案 0 :(得分:0)

当您按下主页按钮时,您的应用程序将进入睡眠状态,基本上它不会关闭。但是,由于在“重新启动”时活动中没有暂停或恢复方法,因此实际上是打开了应用程序的原始实例。这导致错误:

 Caused by: java.lang.IllegalStateException: Activity has been destroyed

由于您的活动不再可用。

答案 1 :(得分:0)

我有同样的问题,最后,我找到了解决方案,当点击“主页”键时,应用程序将在onStop中,但点击“返回”键,应用程序将在onDestroy中,您可以参考{ {3}}

因此,当应用程序调用onDestroy()时,我会杀死与应用程序的任何关系进程。该应用程序现在工作正常。那是我的代码。在FragmentActivity类中

@Override
public void onDestroy() {
    super.onDestroy();
    Log.e(TAG, "--- ON DESTROY ---");
    killAppliactionProcess();
}

// Kill and relation process with the app
private void killAppliactionProcess() {
    Process.killProcess(Process.myPid());
}

答案 2 :(得分:0)

试试这个:

private void setFragmentToTop(String tag) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);

        Fragment fragment = fm.findFragmentByTag(tag);
        if (fragment == null) {
            if (tag.equals(FragmentOne.TAG)) {
                fragment = FragmentOne.newInstance();
            }
            else if (tag.equals(FragmentTwo.TAG)) {
                fragment = FragmentTwo.newInstance();
            }
        }
        ft.replace(R.id.layoutContent, fragment, 
                tag);
        ft.commit();    

    }