2014-09-23 101 views
0

我正在開發一個項目,它在mainactivity的左側和右側有兩個滑塊菜單。當左側滑塊打開時,它顯示了四個項目的列表。單擊列表中的一個項目希望被一個fragment.So我通過列表視圖的mainactivity.The碼的整數現在的位置來代替在主活性的FrameLayout如下所示:IllegalStateException活動已被破壞錯誤android

在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(); 
      } 
     }); 


    } 
} 

的leftfragment:

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; 
    } 


} 

我從左邊的片段調用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(); 

我看到被問及同樣的錯誤幾個問題,但我還沒有發現答案尚未解決。請幫助。

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); 
      } 


     } 
     } 
+0

哪裏是這條線'fragmentTransaction.commit();'?? – 2014-09-23 04:41:29

+0

顯示'displayfragment'方法的代碼也... – 2014-09-23 04:42:43

+0

fragmentTransaction.commit();在displayfragment方法中:參見編輯 – 2014-09-23 04:43:27

回答

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); 
} 
相關問題