无法实现搜索框

时间:2014-05-03 15:39:31

标签: android search searchview

我是新来的,希望你能帮助我。我有一个应用程序,它使用remote.xml进行数据。现在我想实现一个搜索对话框来搜索并返回结果。但是我遇到了一些问题。我遵循了2个指南http://developer.android.com/guide/topics/search/search-dialog.htmlhttp://developer.android.com/training/search/setup.html(我不确定哪个更适用,因为它似乎有相互矛盾的信息)。我想要的只是实现一个搜索框(而不是小部件)。

我设法实现了搜索对话框,点击后会打开它。但是,在添加以下代码后,它在应用程序启动时崩溃(它在setSearchableInfo上调试,而logcat没有显示任何重要信息。我迷路了。

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;
}

我的清单

<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>    

我的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>

我的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"/>

我在列表活动中添加了以下活动

@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()));

感谢有人帮我解决这个问题。谢谢。

LogCat错误日志如下:

05-04 00:28:54.675:E / ActivityThread(3813):无法找到com.google.plus.platform的提供商信息 05-04 00:28:55.405:E / AndroidRuntime(3813):致命异常:主要 05-04 00:28:55.405:E / AndroidRuntime(3813):java.lang.NullPointerException 05-04 00:28:55.405:E / AndroidRuntime(3813):at com.fappbulously.movi​​e.MIListActivity.setUpSearchView(MIListActivity.java:267) 05-04 00:28:55.405:E / AndroidRuntime(3813):at com.fappbulously.movi​​e.MIListActivity.onCreateOptionsMenu(MIListActivity.java:249) 05-04 00:28:55.405:E / AndroidRuntime(3813):在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):在android.os.Handler.handleCallback(Handler.java:605) 05-04 00:28:55.405:E / AndroidRuntime(3813):在android.os.Handler.dispatchMessage(Handler.java:92) 05-04 00:28:55.405:E / AndroidRuntime(3813):在android.os.Looper.loop(Looper.java:137) 05-04 00:28:55.405:E / AndroidRuntime(3813):在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)

2 个答案:

答案 0 :(得分:1)

似乎您忘记在SearchView的{​​{1}}菜单项上将actionViewClass设置为action_search,因此它为空:

main.xml

答案 1 :(得分:0)

对我来说(因为你还没有发布logcat,请这样做)必须有一些兼容性问题。链接没有指定支持库中的任何东西,可能存在问题。我认为你的应用程序支持低于3.0。并且不要混淆使用seach-dialog界面
 http://developer.android.com/guide/topics/search/search-dialog.html

相关问题