我有一个带有搜索图标的操作栏,按下时会加载一个名为搜索的新片段。加载此片段时,我希望顶部的搜索栏展开并打开键盘,以便用户可以键入和搜索。我的onCreateOptionsMenu看起来像这样:
@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>
答案 0 :(得分: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>