Android:IllegalStateException:活动已被破坏

时间:2014-02-14 08:11:43

标签: java android android-fragments

我在stackoverflow上看过很多类似的问题,但我无法弄清楚我做错了什么。我做了一个非常简单的应用程序来复制错误。

该应用程序执行以下操作:
它在MainActivty的onCreate中添加了一个片段 用户可以从对话框中添加另一个片段 当对话框调用MainActivity的addFragment方法时,会在事务提交时抛出异常。

这是MainActivity

package com.example.testapp;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;

public class MainActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final Button button = (Button) findViewById(R.id.add_button);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
             AddButtonDialog addButtonDialog = new AddButtonDialog();
             addButtonDialog.show(getSupportFragmentManager(), "ADD");
        }
    });

    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    CustomFragment fragment = new CustomFragment();
    transaction.add(R.id.fragment_container, fragment);
    transaction.commit();
}


public void addButton() {
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    CustomFragment fragment = new CustomFragment();
    transaction.add(R.id.fragment_container, fragment);
    transaction.commit(); // <-- Exception

}

}

这是调用MainActivity的AddButtonDialog

 public class AddButtonDialog  extends DialogFragment {
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        final LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.new_button_dialog, null);
        builder.setView(view)

               .setPositiveButton("Add", new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int id) {
                       MainActivity main = new MainActivity();
                       main.addButton();
                   }
               });
        return builder.create();   
    }
}

这是stacktrace

    02-14 10:09:27.781: E/AndroidRuntime(26291): FATAL EXCEPTION: main
    02-14 10:09:27.781: E/AndroidRuntime(26291): java.lang.IllegalStateException: Activity has been destroyed
    02-14 10:09:27.781: E/AndroidRuntime(26291):    at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1365)
    02-14 10:09:27.781: E/AndroidRuntime(26291):    at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
    02-14 10:09:27.781: E/AndroidRuntime(26291):    at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
    02-14 10:09:27.781: E/AndroidRuntime(26291):    at com.example.testapp.MainActivity.addButton(MainActivity.java:35)
    02-14 10:09:27.781: E/AndroidRuntime(26291):    at com.example.testapp.AddButtonDialog$1.onClick(AddButtonDialog.java:22)
    02-14 10:09:27.781: E/AndroidRuntime(26291):    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:185)
    02-14 10:09:27.781: E/AndroidRuntime(26291):    at android.os.Handler.dispatchMessage(Handler.java:99)
    02-14 10:09:27.781: E/AndroidRuntime(26291):    at android.os.Looper.loop(Looper.java:137)
    02-14 10:09:27.781: E/AndroidRuntime(26291):    at android.app.ActivityThread.main(ActivityThread.java:5328)
    02-14 10:09:27.781: E/AndroidRuntime(26291):    at java.lang.reflect.Method.invokeNative(Native Method)
    02-14 10:09:27.781: E/AndroidRuntime(26291):    at java.lang.reflect.Method.invoke(Method.java:511)
    02-14 10:09:27.781: E/AndroidRuntime(26291):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
    02-14 10:09:27.781: E/AndroidRuntime(26291):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
    02-14 10:09:27.781: E/AndroidRuntime(26291):    at dalvik.system.NativeStart.main(Native Method)

我很确定我没有抓住Fragment生命周期的概念。请某人如此善意地解释我做错了什么。

4 个答案:

答案 0 :(得分:1)

MainActivity的引用传递给Dialog并尝试此

mainActivity.addButton()

其中mainActivity是参考

答案 1 :(得分:0)

MainActivity main = new MainActivity();
                   main.addButton();

在Android中启动活动的方法是通过startActivity(intent);方法

所以例如改为:

Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);

请参阅Intent Guide

答案 2 :(得分:0)

如果您的AddButtonDialog是一个内部类,那么只需调用addActivity而不是main.addButton()。

如果没有,则尝试在单独的类中创建此方法,并将FragmentManager作为参数传递给它。因此,您可以从任何地方调用此方法来创建新创建的类的实例。

另外,请尝试在addButton方法中使用以下内容

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
CustomFragment fragment = new CustomFragment();
transaction.replace(R.id.fragment_container, fragment); //this will replace your fragment_container with this fragment
transaction.addToBackStack(null);// this will help you get back to your first  fragment on back press
transaction.commit();

答案 3 :(得分:0)

如果你想要做的就是从其他地方调用你的Activity中的方法:
1 - 在主活动中创建一个静态变量,类型为Activity,并在onCreate中将其实例化为此变量,以便存储实例。
2 - 在主活动中创建一个名为get instance的静态方法,它只返回活动的实例
3 - 无论你在哪里打电话:MainActivity.getInstance().yourMethod();
当然,只有你的活动存在才会有效!