سؤال

I am new here and hope you can help me out. I have a an app, which uses remote.xml for data. Now I want to implement a search dialog to search and return results. However I face some issue. I have followed the 2 guides http://developer.android.com/guide/topics/search/search-dialog.html and http://developer.android.com/training/search/setup.html (I am not sure which one is more applicable as it seems it has conflicting info). All I want is to implement a search box (and not the widget).

I have managed to implement the Search Dialog and it opens when clicked. However, after added the following code, it crashed when the app was launched (it debug at the setSearchableInfo and logcat doesnt show any siginicant info. I am lost.

public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.options_menu, menu);

    // Associate searchable configuration with the SearchView
    SearchManager searchManager =
           (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView =
            (SearchView) menu.findItem(R.id.search).getActionView();
    **searchView.setSearchableInfo(
            searchManager.getSearchableInfo(getComponentName()));**

    return true;
}

My Manifest

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >     

        <activity android:name="com.fab.mov.MIListActivity"
            android:screenOrientation="portrait"
            android:launchMode="singleTop">
             <intent-filter>
                   <action android:name="android.intent.action.SEARCH" />
                 </intent-filter>

            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable" />
            <meta-data
                android:name="android.app.default_searchable"
                android:value="com.fab.mov.MIListActivity" />
         </activity>    

My searchable.xml

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_name"
    android:hint="@string/search_hint" >
</searchable>

My main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    xmlns:tools="http://schemas.android.com/tools">


    <item android:id="@+id/action_settings"
        android:title="@string/action_settings"
        android:orderInCategory="100"
        android:showAsAction="never" />

    <item android:id="@+id/action_search"
          android:title="@string/app_label"
          android:showAsAction="ifRoom|withText"/>

And I added the following activity in the list activity

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.feed_list);

        //setContentView(R.layout.action_search);

        // Get the intent, verify the action and get the query
        Intent intent = getIntent();
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
          String query = intent.getStringExtra(SearchManager.QUERY);
        //  doMySearch(query);
        }

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, 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()));

Appreciate someone the help me on this. Thank you.

LogCat error log as follows:

05-04 00:28:54.675: E/ActivityThread(3813): Failed to find provider info for com.google.plus.platform 05-04 00:28:55.405: E/AndroidRuntime(3813): FATAL EXCEPTION: main 05-04 00:28:55.405: E/AndroidRuntime(3813): java.lang.NullPointerException 05-04 00:28:55.405: E/AndroidRuntime(3813): at com.fappbulously.movie.MIListActivity.setUpSearchView(MIListActivity.java:267) 05-04 00:28:55.405: E/AndroidRuntime(3813): at com.fappbulously.movie.MIListActivity.onCreateOptionsMenu(MIListActivity.java:249) 05-04 00:28:55.405: E/AndroidRuntime(3813): at android.app.Activity.onCreatePanelMenu(Activity.java:2445) 05-04 00:28:55.405: E/AndroidRuntime(3813): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:401) 05-04 00:28:55.405: E/AndroidRuntime(3813): at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:767) 05-04 00:28:55.405: E/AndroidRuntime(3813): at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:2922) 05-04 00:28:55.405: E/AndroidRuntime(3813): at android.os.Handler.handleCallback(Handler.java:605) 05-04 00:28:55.405: E/AndroidRuntime(3813): at android.os.Handler.dispatchMessage(Handler.java:92) 05-04 00:28:55.405: E/AndroidRuntime(3813): at android.os.Looper.loop(Looper.java:137) 05-04 00:28:55.405: E/AndroidRuntime(3813): at android.app.ActivityThread.main(ActivityThread.java:4447) 05-04 00:28:55.405: E/AndroidRuntime(3813): at java.lang.reflect.Method.invokeNative(Native Method) 05-04 00:28:55.405: E/AndroidRuntime(3813): at java.lang.reflect.Method.invoke(Method.java:511) 05-04 00:28:55.405: E/AndroidRuntime(3813): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 05-04 00:28:55.405: E/AndroidRuntime(3813): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 05-04 00:28:55.405: E/AndroidRuntime(3813): at dalvik.system.NativeStart.main(Native Method)

هل كانت مفيدة؟

المحلول

Seems that you forgot to set the SearchView as actionViewClass on your action_search menu item in main.xml, so it's null:

<item android:id="@+id/action_search"
      android:title="@string/search"
      android:icon="@drawable/ic_action_search"
      android:showAsAction="ifRoom|collapseActionView"
      android:actionViewClass="android.widget.SearchView" />

نصائح أخرى

Seems to me (because you have not posted the logcat, please do it) there must be some compatibility issue.The links have not specified any thing from support library and there could lie the issue. I think your app is supporting less than 3.0. And Dont be confused used the seach-dialog interface
http://developer.android.com/guide/topics/search/search-dialog.html

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top