Reputation: 71
I would like to add a search function in the action bar following this as a guide but I still got these errors.
07-02 13:36:05.175 21813-21813/com.example.fieldbookv2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.fieldbookv2, PID: 21813
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SearchView.setSearchableInfo(android.app.SearchableInfo)' on a null object reference
at com.example.fieldbookv2.LoginActivity.onCreateOptionsMenu(LoginActivity.java:1621)
at android.app.Activity.onCreatePanelMenu(Activity.java:2823)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:277)
at android.support.v7.internal.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:84)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:273)
at android.support.v7.app.AppCompatDelegateImplV7.preparePanel(AppCompatDelegateImplV7.java:1111)
at android.support.v7.app.AppCompatDelegateImplV7.doInvalidatePanelMenu(AppCompatDelegateImplV7.java:1396)
at android.support.v7.app.AppCompatDelegateImplV7.access$100(AppCompatDelegateImplV7.java:89)
at android.support.v7.app.AppCompatDelegateImplV7$1.run(AppCompatDelegateImplV7.java:126)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
This is my main_activity_action.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/tools">
<item android:id="@+id/action_search"
android:title="Search"
android:icon="@drawable/ic_search_white"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.widget.SearchView"/>
</menu>
searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/search"
android:hint="@string/enter" />
LoginActivity.java
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search) .getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return super.onCreateOptionsMenu(menu);
}
and AndroidManifest.xml
<activity
android:name="com.example.fieldbookv2.LoginActivity"
android:label="@string/title_activity_login" >
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchResultsActivity" />
</activity>
Any idea why it says null reference?
Upvotes: 1
Views: 408
Reputation: 71
Fixed the error. The main_actions_menu.xml should be like this
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_search"
android:title="Search"
android:icon="@drawable/ic_search_white"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.widget.SearchView"/>
</menu>
remove unnecessary tags and changed "yourapp" to "app since app is the one assigned.
Upvotes: 1
Reputation: 856
Try This and I am Using EditText not actionbar:
<EditText
android:id="@+id/members_search"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="10dp"
android:ems="8"
android:singleLine="true"
android:background="@drawable/round_corner_edittext"
android:hint="Search Box" />
In class:
members_search.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
adapter_members.getFilter().filter(s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
Upvotes: 0