SearchView为null [重复]

问题描述 投票:-1回答:2

这个问题在这里已有答案:

我想在app栏中使用SearchView。当我运行它时会出现以下错误:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: in.backtogodhead, PID: 1876
                  java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.SearchView.setSearchableInfo(android.app.SearchableInfo)' on a null object reference
                      at in.backtogodhead.MainActivity.onCreateOptionsMenu(MainActivity.java:590)
                      at android.app.Activity.onCreatePanelMenu(Activity.java:2823)
                      at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:328)
                      at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:93)
                      at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:332)
                      at android.support.v7.app.AppCompatDelegateImplV9.preparePanel(AppCompatDelegateImplV9.java:1370)
                      at android.support.v7.app.AppCompatDelegateImplV9.doInvalidatePanelMenu(AppCompatDelegateImplV9.java:1650)
                      at android.support.v7.app.AppCompatDelegateImplV9$1.run(AppCompatDelegateImplV9.java:134)
                      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)

以下是详细信息:

/热水/XML/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>

AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="test.test">

    <application
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">

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


            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>

        <activity
            android:name=".SearchActivity"
            android:label="@string/title_activity_search"
            android:theme="@style/AppTheme">

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

        </activity>

    </application>

</manifest>

/menu/menu_卖弄.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" >
    <item android:id="@+id/search"
        android:title="search_title"
        android:icon="@android:drawable/ic_search_category_default"
        app:showAsAction="ifRoom"
        app:actionViewClass="android.support.v7.widget.SearchView" />

</menu>

我的MainActivity.java

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Here shows the MainActivity content

    }


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.menu_main, menu);
    SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
    SearchManager searchManager =
            (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    searchView.setSearchableInfo(
            searchManager.getSearchableInfo(getComponentName()));
    return true;
}

我的SearchActivity.java

public class SearchActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_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);
            Toast tag = Toast.makeText(getApplicationContext(), "Testing",Toast.LENGTH_SHORT);
            tag.show();
        }
    }


}

styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:windowDisablePreview">true</item>
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme" parent="AppBaseTheme">
        <item name="actionOverflowMenuStyle">@style/OverflowMenu</item>
    </style>

</resources>

pro gaur的-如来色身.pro

-keep class android.support.v7.widget.SearchView { *; }

在我的问题中,我正在做一切正确,但仍然显示错误。所以我无法弄明白

android searchview
2个回答
0
投票

我认为你的问题是你使用

返回findViewById(R.id.search)null

相反,你应该使用

MenuItem searchItem = menu.findItem(R.id.search);

之后

SearchView searchView = MenuItem.getActionView(searchItem);

0
投票

看到这会对你有所帮助

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

RES /菜单/ options_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/search"
      android:title="@string/search_title"
      android:icon="@drawable/ic_search"
      android:showAsAction="collapseActionView|ifRoom"
      android:actionViewClass="android.widget.SearchView" />
</menu>

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_name"
    android:hint="@string/search_hint" />

在清单文件中

<activity ... >
...
<meta-data android:name="android.app.searchable"
        android:resource="@xml/searchable" />

</activity>

<activity android:name=".SearchResultsActivity" ... >
...
<intent-filter>
    <action android:name="android.intent.action.SEARCH" />
</intent-filter>
...
</activity>
© www.soinside.com 2019 - 2024. All rights reserved.