android searchview getSearchableInfo抛出nullpointer异常

时间:2015-03-14 05:49:37

标签: java android

我尝试使用searchview小部件在MainActivity中实现示例搜索界面。但是它一直在MainActivity的onCreateOptionsMenu方法中的getSearchableInfo中抛出空指针异常。

我已经尝试了以下方法,但没有任何帮助。它只是一个示例代码,它让我很难过。:

  • 在菜单xml项目和MainActivity的onCreateOptionsMenu中使用android.support.v7.widget.SearchView或使用android.widget.SearchView。
  • 使用MenuItemCompat或使用onCreateOptionsMenu中的菜单。
  • 只使用getComponentName()或使用新的ComponentName(this,SearchActivity.class)。
  • 在AndroidManifest中使用完整路径或使用SearchActivity的相对路径。
  • 在proguard-android.txt中添加以下行:-keep class android.support.v7.widget.SearchView {*; }

这是stacktrace:

Process: com.mycompany.testsearch, PID: 17480
java.lang.NullPointerException
        at com.mycompany.testsearch.MainActivity.onCreateOptionsMenu(MainActivity.java:34)
        at android.app.Activity.onCreatePanelMenu(Activity.java:2546)
        at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:275)
        at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:276)
        at android.support.v7.app.ActionBarActivityDelegate$1.onCreatePanelMenu(ActionBarActivityDelegate.java:79)
        at android.support.v7.app.ActionBarActivityDelegateBase.preparePanel(ActionBarActivityDelegateBase.java:979)
        at android.support.v7.app.ActionBarActivityDelegateBase.doInvalidatePanelMenu(ActionBarActivityDelegateBase.java:1182)
        at android.support.v7.app.ActionBarActivityDelegateBase.access$100(ActionBarActivityDelegateBase.java:79)
        at android.support.v7.app.ActionBarActivityDelegateBase$1.run(ActionBarActivityDelegateBase.java:118)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5086)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)

以下是我的代码文件:

1] AndroidManifest:

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

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

    <meta-data
        android:name="android.app.default_searchable"
        android:value=".SearchableActivity" />
    <activity
        android:name=".SearchableActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <meta-data android:name="android.app.searchable"
            android:resource="@xml/searchable"
            android:value=".activities.SearchableActivity" />
    </activity>
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

2] Main_menu.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:TestSearch="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/my_exp_search"
    android:title="action bar search"
    android:icon="@android:drawable/ic_menu_search"
    TestSearch:showAsAction="always"
    TestSearch:actionViewClass="android.support.v7.widget.SearchView" />
<item
    android:id="@+id/action_settings"
    android:title="@string/action_settings"
    android:orderInCategory="100"
    TestSearch:showAsAction="never" />
</menu>

3] 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/hint_text"
android:includeInGlobalSearch="true">
</searchable>

4] MainActivity:

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
@Override
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);
//SearchManager searchManager =     
//(SearchManager)getSystemService(Context.SEARCH_SERVICE);
//SearchView searchView =      
//(SearchView)menu.findItem(R.id.my_exp_search).getActionView();
    MenuItem searchItem = menu.findItem(R.id.my_exp_search);
    SearchView searchView = 
   (SearchView)MenuItemCompat.getActionView(searchItem);         
searchView.setSearchableInfo(
       searchManager.getSearchableInfo(getComponentName()));
//        String pkg = "com.mycompany.testsearch";
//        String cls = "com.mycompany.testsearch.SearchActivity";
//        ComponentName mycomponent = new ComponentName(pkg,cls);             
searchView.setSearchableInfo(
          searchManager.getSearchableInfo(mycomponent));
 searchView.setIconifiedByDefault(false);
 return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
 }
}

5] SearchActivity:

public class SearchableActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_searchable);
    Intent intent = getIntent();
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        Log.d("test","Reached searchable");
        String query = intent.getStringExtra(SearchManager.QUERY);
        TextView textView = 
               (TextView)findViewById(R.id.text_message);
        textView.setText(query);
    }
}
@Override
protected void onNewIntent(Intent intent) {
    setIntent(intent);
    handleIntent(intent);
}
private void handleIntent(Intent intent) {
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        TextView textView = 
                (TextView)findViewById(R.id.text_message);
        textView.setText(query);
    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is     
//present.
    getMenuInflater().inflate(R.menu.menu_searchable, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

请帮我解决这个问题。

0 个答案:

没有答案