Android Fragment - 运行时异常 - 活动已被破坏

时间:2014-06-19 14:53:49

标签: android class android-activity fragment

我从我的MainActivity开始

OSMdroidFragment Map = new OSMdroidFragment();
Map.setFragment();

新课程。方法setFragment用我的frag启动另一个类。

public class OSMdroidFragment extends MainActivity
{
    void setFragment() 
    {
        map = true;
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        MapFragment frag = new MapFragment();
        fragmentTransaction.replace(R.id.main_view, frag);
        fragmentTransaction.commit();
    }
    public class MapFragment extends Fragment
    {
        public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
        {
            View mainView       = inflater.inflate(R.layout.mainview, container, false);

在命令fragmentTransaction.commit();之后我收到错误:

06-19 16:34:33.661: E/AndroidRuntime(7346): FATAL EXCEPTION: main
06-19 16:34:33.661: E/AndroidRuntime(7346): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.thomas_smart/com.example.thomas_smart.MainActivity}: java.lang.IllegalStateException: Activity has been destroyed

我不知道我能做什么。我只找到了这个链接enter link description here ...有人有想法吗?

以下是完整的错误列表:

06-19 16:34:33.661: E/AndroidRuntime(7346): FATAL EXCEPTION: main

06-19 16:34:33.661: E/AndroidRuntime(7346): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.thomas_smart/com.example.thomas_smart.MainActivity}: java.lang.IllegalStateException: Activity has been destroyed

06-19 16:34:33.661: E/AndroidRuntime(7346):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
06-19 16:34:33.661: E/AndroidRuntime(7346):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)

06-19 16:34:33.661: E/AndroidRuntime(7346):     at android.app.ActivityThread.access$700(ActivityThread.java:140)

06-19 16:34:33.661: E/AndroidRuntime(7346):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)

06-19 16:34:33.661: E/AndroidRuntime(7346):     at android.os.Handler.dispatchMessage(Handler.java:99)

06-19 16:34:33.661: E/AndroidRuntime(7346):     at android.os.Looper.loop(Looper.java:137)

06-19 16:34:33.661: E/AndroidRuntime(7346):     at android.app.ActivityThread.main(ActivityThread.java:4921)

06-19 16:34:33.661: E/AndroidRuntime(7346):     at java.lang.reflect.Method.invokeNative(Native Method)

06-19 16:34:33.661: E/AndroidRuntime(7346):     at java.lang.reflect.Method.invoke(Method.java:511)

06-19 16:34:33.661: E/AndroidRuntime(7346):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)

06-19 16:34:33.661: E/AndroidRuntime(7346):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)

06-19 16:34:33.661: E/AndroidRuntime(7346):     at dalvik.system.NativeStart.main(Native Method)

06-19 16:34:33.661: E/AndroidRuntime(7346): Caused by: java.lang.IllegalStateException: Activity has been destroyed

06-19 16:34:33.661: E/AndroidRuntime(7346):     at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1295)

06-19 16:34:33.661: E/AndroidRuntime(7346):     at android.app.BackStackRecord.commitInternal(BackStackRecord.java:548)

06-19 16:34:33.661: E/AndroidRuntime(7346):     at android.app.BackStackRecord.commit(BackStackRecord.java:532)

06-19 16:34:33.661: E/AndroidRuntime(7346):     at com.example.thomas_smart.OSMdroidFragment.setFragment(OSMdroidFragment.java:49)

06-19 16:34:33.661: E/AndroidRuntime(7346):     at com.example.thomas_smart.MainActivity.selectDrawerItem(MainActivity.java:361)

06-19 16:34:33.661: E/AndroidRuntime(7346):     at com.example.thomas_smart.MainActivity.onCreate(MainActivity.java:303)

06-19 16:34:33.661: E/AndroidRuntime(7346):     at android.app.Activity.performCreate(Activity.java:5206)

06-19 16:34:33.661: E/AndroidRuntime(7346):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)

06-19 16:34:33.661: E/AndroidRuntime(7346):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
06-19 16:34:33.661: E/AndroidRuntime(7346):     ... 11 more

我发现了一条新的错误消息:

android.content.ActivityNotFoundException:无法找到显式活动类{com.example.thomas_smart / com.example.thomas_smart.OSMdroidFragment};你有没有在AndroidManifest.xml中声明这个活动?

我必须在清单中创建一个新的活动?我不想开始新的活动...

1 个答案:

答案 0 :(得分:0)

您可以尝试将活动扩展为FragmentActivity

另外,请尝试使用fragmentTransaction.add(...)方法!

希望这会有所帮助!!