2015-06-22 53 views
0

我正在使用一個導航抽屜,其中我使用了多個片段。在其中一個片段中,我必須使用某個動作的菜單項。現在,我可以在操作欄上充氣菜單項在該片段上,但行動沒有執行。操作欄菜單項不能在片段中工作

public class Location extends Fragment implements View.OnClickListener { 

    GoogleMap googleMap; 
    Fragment fragment; 
    Button arrived_mbtn; 
    TextView current_mtv,request_mtv; 
    LinearLayout btn_mlayout,journey_mlayout; 
    View rootView; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 

     rootView = inflater.inflate(R.layout.fragment_location, container, false); 
     return rootView; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Log.e("you are in oncreate", "dfsdfsd"); 
     setHasOptionsMenu(true); 

    } 

    @Override 
    public void onDestroyView() { 
     // TODO Auto-generated method stub 



     try { 

      if (fragment != null) { 

       fragment = getFragmentManager().findFragmentById(R.id.map); 

       FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction(); 
       ft.remove(fragment); 
       ft.commit(); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     Thread.interrupted(); 
     super.onPause(); 
    } 


    @Override 
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 
     super.onCreateOptionsMenu(menu, inflater); 
     getActivity().getMenuInflater().inflate(R.menu.menu_payment_card_detail,menu); 

    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) 
     { 
      case R.id.action_settings: 
       Toast.makeText(getActivity(),"hello",Toast.LENGTH_SHORT); 


      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
} 

通過此代碼一個項目是在操作欄上膨脹,但點擊它沒有顯示任何東西。

回答

0

像這樣變化。你沒有在烤麪包上調用show()。

case R.id.action_settings: 
    Toast.makeText(getActivity(),"hello",Toast.LENGTH_SHORT).show(); 
+0

兄弟我也檢查了logcat的.. –

+0

有一個在代碼中沒有日誌消息......曾經嘗試與上述溶液... – Chandrakanth

+0

兄弟從三天進出口工作在這.. –

0

更改您的onCreateOptionsMenu方法,如下所示。

@Override 
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 
     inflater.inflate(R.menu.menu_payment_card_detail,menu); 
    } 

而且無論您已應用於您的菜單項的標識,在條件中使用它來檢查該菜單項是否被點擊。

在你的代碼
0

  • 移動setHasOptionsMenu(true)onCreateView()方法;

  • 使用inflater.inflate(R.menu.menu_payment_card_detail,menu);onCreateOptionsMenu方法。

事情是這樣的:

@Override 
     public void onCreateOptionsMenu (Menu menu, MenuInflater inflater) { 
      inflater.inflate(R.menu.menu_payment_card_detail,menu); 
      super.onCreateOptionsMenu(menu, inflater); 
     } 
  • 添加show()方法來給你的麪包Toast.makeText(getActivity(),"hello",Toast.LENGTH_SHORT).show();
-2

檢查父活動不吃選擇的項目,即讓確定父級活動是否實現onOptionsItemSelected()它對於片段的項目返回false。

0

在片段:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    // set setHasOptionsMenu 
    setHasOptionsMenu(true); 
} 
+0

雖然此代碼可能會回答問題,但提供其他關於_how_和/或_why_的[上下文](https://meta.stackexchange.com/q/114762)可以解決問題,從而提高答案的長期價值。它也不會提到爲什麼這個答案比別人更合適。 –