2014-02-13 23 views
0

我有一個帶有搜索圖標的操作欄,當它按下時會加載一個名爲search的新片段。當這個片段被加載時,我想讓頂部的搜索欄展開並打開鍵盤,以便用戶可以輸入和搜索。我有我的onCreateOptionsMenu看起來像這樣:oncreateMenuOptions沒有從Overide中調用

@Override 
    public void onCreateOptionsMenu (Menu menu, MenuInflater inflater) { 
     // Inflate the menu; this adds items to the action bar if it is present. 

     Log.d("click" , "inside the on create"); 

     getActivity().getMenuInflater().inflate(R.menu.main, menu); 
     SearchView searchView = (SearchView) menu.findItem(R.id.menu_search2).getActionView(); 
     searchView.setIconified(false); 
     searchView.setOnQueryTextListener(this); 
    } 

所有碎片的代碼是:

import android.app.Fragment; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.preference.PreferenceManager; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ListView; 
import android.widget.SearchView; 

import java.io.UnsupportedEncodingException; 
import java.net.URLEncoder; 

/** 
* Created by Mike on 2/13/14. 
*/ 

public class Search extends Fragment implements SearchView.OnQueryTextListener { 

    private ListView lv; 
    View v; 

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

     //set layout here 
     v = inflater.inflate(R.layout.activity_search, container, false); 


     //get user information 
     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); 
     String userName = prefs.getString("userName", null); 
     String userID = prefs.getString("userID", null); 


     //todo: code body goes here 





     // Inflate the layout for this fragment 
     return v; 


} 


    @Override 
    public void onCreateOptionsMenu (Menu menu, MenuInflater inflater) { 
     // Inflate the menu; this adds items to the action bar if it is present. 

     Log.d("click" , "inside the on create"); 

     getActivity().getMenuInflater().inflate(R.menu.main, menu); 
     SearchView searchView = (SearchView) menu.findItem(R.id.menu_search2).getActionView(); 
     searchView.setIconified(false); 
     searchView.setOnQueryTextListener(this); 
    } 




    public boolean onQueryTextSubmit (String query) { 

     //toast query 

     //make json variables to fill 

     // url to make request 
     String url = "myURL"; 



     try { 
      query = URLEncoder.encode(query, "UTF-8"); 
     } catch (UnsupportedEncodingException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 


     String jsonUrl = url + query; 



     //todo: get json 
     new ReadJSONResult(getActivity()).execute(jsonUrl); 

     return false; 
    } 





    @Override 
    public boolean onQueryTextChange(String newText) { 
     // TODO Auto-generated method stub 
     return false; 
    } 





} 

更新:我添加

代碼爲

setHasOptionsMenu(true); 

這是FC

02-13 17:11:11.093 21151-21151/com.beerportfolio.beerportfoliopro E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    java.lang.NullPointerException 
      at com.beerportfolio.beerportfoliopro.Search.onCreateOptionsMenu(Search.java:66) 
      at android.app.Fragment.performCreateOptionsMenu(Fragment.java:1868) 
      at android.app.FragmentManagerImpl.dispatchCreateOptionsMenu(FragmentManager.java:2068) 
      at android.app.Activity.onCreatePanelMenu(Activity.java:2567) 
      at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:485) 
      at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:851) 
      at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:270) 
      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:786) 
      at android.view.Choreographer.doCallbacks(Choreographer.java:586) 
      at android.view.Choreographer.doFrame(Choreographer.java:545) 
      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:771) 
      at android.os.Handler.handleCallback(Handler.java:730) 
      at android.os.Handler.dispatchMessage(Handler.java:92) 
      at android.os.Looper.loop(Looper.java:158) 
      at android.app.ActivityThread.main(ActivityThread.java:5789) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:525) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:843) 
      at dalvik.system.NativeStart.main(Native Method) 

更新3:

按照下面的答案是試圖幫助我。我現在想這:

super.onCreateOptionsMenu(android.R.menu, inflater); 
     setHasOptionsMenu(true); 

但我收到此錯誤:

上android.R.menu

Expression Expected 

的main.xml是:

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:context=".MainActivity" > 

    <item android:id="@+id/menu_search2" 
     android:title="Search" 
     android:onClick="goToSearch" 
     android:icon="@android:drawable/ic_menu_search" 
     android:showAsAction="ifRoom|collapseActionView" 
     /> 


</menu> 

回答

1

您需要告訴您的片段想要參與選項菜單的活動。在onCreateView加入這個()方法:

setHasOptionsMenu(true); 

http://developer.android.com/reference/android/app/Fragment.html#setHasOptionsMenu(boolean)

更新1

你onCreateOptionsMenu方法應該是:

@Override 
    public void onCreateOptionsMenu (Menu menu, MenuInflater inflater) { 
     // Inflate the menu; this adds items to the action bar if it is present. 

     super.onCreateOptionsMenu(menu, inflater); 

     Log.d("click" , "inside the on create"); 

     inflater.inflate(R.menu.main, menu); 
     SearchView searchView = (SearchView) menu.findItem(R.id.menu_search2).getActionView(); 
     searchView.setIconified(false); 
     searchView.setOnQueryTextListener(this); 
    } 

更新2:

您的main.xml也應該需要申報的動作視圖類:

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:context=".MainActivity" > 

    <item android:id="@+id/menu_search2" 
     android:actionViewClass="android.widget.SearchView" 
     android:title="Search" 
     android:onClick="goToSearch" 
     android:icon="@android:drawable/ic_menu_search" 
     android:showAsAction="ifRoom|collapseActionView" 
     /> 
</menu> 
+0

我說這一點,它拉福斯這個錯誤現在關閉: – Mike

+0

加入FC上面的問題。我將這行代碼添加到我的片段中了嗎? – Mike

+0

你的onCreateOptionsMenu現在肯定被調用(你可以在堆棧跟蹤中看到它)。看起來你也需要添加一個調用super.onCreateOptionsMenu(menu,inflater);在該方法的第一行。如果您在此之後仍然收到空指針異常,請仔細檢查以確保在您的分段的佈局中有一個名爲「menu_search2」的ID。 –