简体   繁体   中英

App crashes after adding search widget on appbar

I was looking for a way to add search functionality to my app (specifically on the app bar).

I found a documentation on Andoid devoloper guide

I followed every single step carefully but app crashes.

  1. I Created a Searchable Configuration

res/xml/searchable.xml

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_label"
    android:hint="@string/search_hint" >
</searchable> 
  1. I created a new Searchable Activity called SearchActivity

  2. I declared SearchActivity as Searchable Activty in manifest

Manifest.xml

<application ... >
    <activity android:name=".SearchActivity" >
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <meta-data android:name="android.app.searchable"
                   android:resource="@xml/searchable"/>
    </activity>
    ...
</application>
  1. I Added these in the OncreateOptionsMenu in MainActivity

     public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); // Get the searchview and set the searchable configuration SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView(); // Assumes current activity is the searchable activity searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); searchView.setIconifiedByDefault(false); return true; } 
  2. Here is menu/menu_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/action_search" android:title="@string/action_search" android:orderInCategory="100" android:icon="@drawable/ic_search_white" app:showAsAction="ifRoom" /> 

There are 2 Activitues.

  • The MainActivty (which has the Appbar with the search icon as shown in menu_main.xml file)
  • SearchActivity

When the user clicks on the search icon in MainActivity, I want it to go to the SearchActivity and display the search widget there. How do I do this.

What should I add to SearchActivity.java ??

Here is the log message i got when the app crashed -

    --------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.android.fotos, PID: 2613
                  java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.SearchView.setSearchableInfo(android.app.SearchableInfo)' on a null object reference
                      at com.example.android.fotos.MainActivity.onCreateOptionsMenu(MainActivity.java:41)
                      at android.app.Activity.onCreatePanelMenu(Activity.java:2846)
                      at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:358)
                      at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:88)
                      at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:270)
                      at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:88)
                      at android.support.v7.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:454)
                      at android.support.v7.app.ToolbarActionBar$1.run(ToolbarActionBar.java:61)
                      at android.os.Handler.handleCallback(Handler.java:739)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:148)
                      at android.app.ActivityThread.main(ActivityThread.java:5417)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Try to add

<meta-data
     android:name="android.app.searchable"
     android:value=".SearchActivity" />

in your Manifest.xml inside the application section:

<application ... >
....
<meta-data
     android:name="android.app.searchable"
     android:value=".SearchActivity" />
....
<activity android:name=".SearchActivity" >
    <intent-filter>
        <action android:name="android.intent.action.SEARCH" />
    </intent-filter>
    <meta-data android:name="android.app.searchable"
               android:resource="@xml/searchable"/>
</activity>
...

This tells your application how to solve "android.app.searchable"

Look at this and you will find:

If you want every activity in your application to provide the search dialog, insert the above element as a child of the element, instead of each . This way, every activity inherits the value, provides the search dialog, and delivers searches to the same searchable activity. (If you have multiple searchable activities, you can override the default searchable activity by placing a different declaration inside individual activities.) With the search dialog now enabled for your activities, your application is ready to perform searches.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM