java.lang.NullPointerException SearchView OnCreateOptionsMenu

时间:2015-02-27 19:49:01

标签: android android-support-library android-actionbar-compat android-search android-searchmanager

我正在努力使我的应用程序适用于Gingerbread(API 10)。当我点击菜单并出现以下错误时,我的应用程序崩溃了 -

java.lang.NullPointerException
            at com.sample.appdemo.onCreateOptionsMenu(AppDemoListActivity.java:83)

我的应用程序在过去使用API​​ 11及更高版本时运行良好。有人请帮忙吗?

这就是我所拥有的:

的build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"
    defaultConfig {
        applicationId "com.sample.appdemo"
        minSdkVersion 8
        targetSdkVersion 19
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
    productFlavors {
    }
}

dependencies {
    compile project(':poi-3.10-FINAL-20140208')
    compile 'com.android.support:appcompat-v7:21.0.3'
}

AppDemoListActivity.java

    package ...;
    import android.app.SearchManager;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentTransaction;
    import android.support.v4.view.MenuItemCompat;
    import android.support.v7.widget.SearchView;
    import ...;

    @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.action_bar_search_optionsmenu, menu);
            SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);

        MenuItem searchItem = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);

        ComponentName cn = new ComponentName(this, SearchActivity.class);
        searchView.setSearchableInfo(
                searchManager.getSearchableInfo(cn)
        );

        return super.onCreateOptionsMenu(menu);
    }

action_bar_search_menu:

<?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:icon="@drawable/ic_action_search"
        android:title="@string/action_search"
        app:showAsAction="always"
        app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>

更新#1:

清单:

<activity
    android:name=".SearchActivity"
    android:label="@string/title_activity_search"
    android:parentActivityName=".AppDemoListActivity" >

    <!-- This intent-filter identifies this activity as "searchable" -->

    <intent-filter>
        <action android:name="android.intent.action.SEARCH" />

        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>

    <!-- This metadata entry provides further configuration details for searches -->
    <!-- that are handled by this activity. -->

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

0 个答案:

没有答案