IllegalStateException活动已被破坏android的错误

时间:2014-09-23 04:37:02

标签: java android

我正在开发一个项目,在mainactivity的左侧和右侧有两个滑块菜单。当左侧滑块打开时,它会显示一个包含四个项目的列表。单击列表中的项目我想在主要活动中使用framelayout要被片段替换。所以我将listview的整数位置传递给mainactivity。代码如下所示:

主要活动

package com.navdrawer.demo.simple;

import com.navdrawer.SimpleSideDrawer;
import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends Activity {

    private SimpleSideDrawer mNav;
    Button btn1;
    RightFragment rf;
    ListView lv_countries;
    String[] values;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        values = new String[] { 
             "Android List View", 
                "Adapter implementation",
                "Simple List View In Android",
                "Create List View Android", 
                "Android Example", 
                "List View Source Code", 
                "List View Array Adapter", 
                "Android Example List View" 
               };
        mNav = new SimpleSideDrawer(this);

        mNav.setLeftBehindContentView(R.layout.activity_behind_left_simple);
           findViewById(R.id.leftBtn).setOnClickListener(new OnClickListener() {
            @Override 
            public void onClick(View v) {

                mNav.toggleLeftDrawer();
            }
        });


        mNav.setRightBehindContentView(R.layout.activity_behind_right_simple);
        final LayoutInflater factory = getLayoutInflater();

        final View textEntryView = factory.inflate(R.layout.activity_behind_right_simple, null);

        lv_countries =  (ListView) textEntryView.findViewById(R.id.lv_countries);
        ArrayAdapter<String> adapter =new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,values);
        lv_countries.setAdapter(adapter);

        findViewById(R.id.rightBtn).setOnClickListener(new OnClickListener() {
            @Override 
            public void onClick(View v) { 
                //rf=new RightFragment();
                mNav.toggleRightDrawer();
            }
        });


    }
}

左片段:

package com.example.facebook.slideoutmenu;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
//Left Menu 

public class LeftMenuFragment extends Fragment {

    ListView lv_settings;
    String[] values;
    ArrayAdapter<String> adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.leftmenu, container, false);
        lv_settings=(ListView) view.findViewById(R.id.lv_settings);
        values=new String[]{"Dashboard","View Jobs","Support","About"};
        adapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,values);
        lv_settings.setAdapter(adapter);

        lv_settings.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {


                new MainActivity().displayfragment(arg2);







            }


        });
        return view;
    }


}

我从leftfragment调用mainactivity的displayfragment方法,如下所示:new MainActivity().displayfragment(arg2);

但是我在LOGCAT中收到以下错误:

09-23 09:50:07.078: E/AndroidRuntime(10839): FATAL EXCEPTION: main
09-23 09:50:07.078: E/AndroidRuntime(10839): java.lang.IllegalStateException: Activity has been destroyed
09-23 09:50:07.078: E/AndroidRuntime(10839):    at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1342)
09-23 09:50:07.078: E/AndroidRuntime(10839):    at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
09-23 09:50:07.078: E/AndroidRuntime(10839):    at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
09-23 09:50:07.078: E/AndroidRuntime(10839):    at com.example.facebook.slideoutmenu.MainActivity.displayfragment(MainActivity.java:258)
09-23 09:50:07.078: E/AndroidRuntime(10839):    at com.example.facebook.slideoutmenu.LeftMenuFragment$1.onItemClick(LeftMenuFragment.java:43)
09-23 09:50:07.078: E/AndroidRuntime(10839):    at android.widget.AdapterView.performItemClick(AdapterView.java:284)
09-23 09:50:07.078: E/AndroidRuntime(10839):    at android.widget.ListView.performItemClick(ListView.java:3701)
09-23 09:50:07.078: E/AndroidRuntime(10839):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:1970)
09-23 09:50:07.078: E/AndroidRuntime(10839):    at android.os.Handler.handleCallback(Handler.java:587)
09-23 09:50:07.078: E/AndroidRuntime(10839):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-23 09:50:07.078: E/AndroidRuntime(10839):    at android.os.Looper.loop(Looper.java:130)
09-23 09:50:07.078: E/AndroidRuntime(10839):    at android.app.ActivityThread.main(ActivityThread.java:3689)
09-23 09:50:07.078: E/AndroidRuntime(10839):    at java.lang.reflect.Method.invokeNative(Native Method)
09-23 09:50:07.078: E/AndroidRuntime(10839):    at java.lang.reflect.Method.invoke(Method.java:507)
09-23 09:50:07.078: E/AndroidRuntime(10839):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
09-23 09:50:07.078: E/AndroidRuntime(10839):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
09-23 09:50:07.078: E/AndroidRuntime(10839):    at dalvik.system.NativeStart.main(Native Method)

错误显示在以下行:

fragmentTransaction.commit();

我在同一个错误上看到了几个问题,但我还没找到一个satsifactory答案。请帮忙。

displayfragment方法:

public  void displayfragment(int position)
    {
        switch(position)
        {
        case 0:
        {

            Fragment fragment=new DashboardFragment();
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.menuPanel,fragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
            //MainActivity activity=new MainActivity();


            if(isExpanded)
            {
                isExpanded = false;
                // Collapse


                new CollapseAnimation(slidingPanel, panelWidth1,
                        TranslateAnimation.RELATIVE_TO_SELF, 0.75f,
                        TranslateAnimation.RELATIVE_TO_SELF, 0.0f, 0, 0.0f,
                        0, 0.0f);
            } 


        }
        }

1 个答案:

答案 0 :(得分:0)

您的LeftMenuFragment正在onItemClick中创建一个新的MainActivity。这不起作用。

Android代表您创建活动,它应该已经运行并附加到您的片段。因此,您应该使用Fragment提供的getActivity()方法来检索您的MainActivity。

我假设您的MainActivity中定义了“displayfragment”方法?如果你需要调用任何类似的自定义方法,只需将getActivity()的结果首先强制转换为MainActivity:

MainActivity mainActivity = (MainActivity)getActivity();

if (mainActivity != null) // Make sure we are attached 
{
    mainActivity.displayFragment(arg2);
}