2014-02-25 79 views
0

如何從onOptionsItemsSelected啓動片段?操作欄啓動片段

@Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      case R.id.action_support: 

       Fragment f = new SupportFragment(); 
          FragmentManager fragmentManager = getFragmentManager(); 
       fragmentManager.beginTransaction() 
         .replace(android.R.id.content, f).commit(); 



       Toast display = Toast.makeText(this, "Settings", 10); 
       display.show(); 
      case R.id.action_guide: 
       Toast display1 = Toast.makeText(this, "Guide", 10); 
       display1.show(); 
      return true; 
      default: 
      return super.onOptionsItemSelected(item); 
     } 
    } 

} 

我試圖啓動支持片段。

感謝

+0

https://www.dropbox.com/s/4vnzxmypv6hggvh/error.txt – user2229747

+1

顯示java.lang.NullPointerException 02-25 13:06 :37.231:W/System.err(27967):\t at openday.SupportFragment.onCreate(SupportFragment.java:18) 查看SupportFragment.java類中的第18行有一個nullPointerException,或編輯您的帖子併發布整個代碼來獲得t他的問題解決了,但這似乎是另一個問題 –

+0

謝謝,發現問題! – user2229747

回答

4
@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    Fragment fragment; 
    switch (item.getItemId()) { 
      case R.id.action_support: 
       fragment = new Home(); 
      case 1: 
       fragment = new NotesList(); 
      default: 
       fragment = new defaultPageOrAnyOtheOption(); 
    } 

    FragmentManager fragmentManager = getFragmentManager(); 
    fragmentManager.beginTransaction() 
      .replace(R.id.content_frame, fragment).commit(); 
    return true; 

} // onOptionsItemSelected 
+0

嗨,感謝您的代碼,我已經更新了我原來的帖子與代碼和一個鏈接到logcat輸出,因爲它仍然崩潰的應用程序 – user2229747

0

試試吧

@Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      case R.id.action_support: 

       //launch Support Fragment 
      // Fragment f = new SupportFragment(); 
      // replace(android.R.id.content, f); 

       Toast display = Toast.makeText(this, "Settings", 10); 
       display.show(); 
      return true; 
      case R.id.action_guide: 
       Toast display1 = Toast.makeText(this, "Guide", 10); 
       display1.show(); 
      return true; 
        } 
    } 
+0

它不起作用 – user2229747

+0

可以發佈您的日誌貓.. –

+0

日誌貓:https://www.dropbox.com/s/4vnzxmypv6hggvh/error.txt – user2229747