SearchView java.lang.NullPointerException

时间:2014-01-30 17:58:00

标签: java android xml nullpointerexception searchview

我对Android比较陌生,我无法让我的项目工作。 我只想显示从我的主要活动的搜索字段传递到另一个名为SearchResultsActivity的活动的数据(感谢log.v)。我正在关注the official android guide here这样做。

但是,当我启动应用程序时,我在MainActivity.java的logcat中获得了一个 SearchView java.lang.NullPointerException

SearchView searchView = (SearchView) menu.getItem(R.id.searchfield).getActionView();



MainActivity.java:

package com.example.fidbacks_search;

import android.os.Bundle;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.view.Menu;
import android.widget.SearchView;

public class MainActivity extends Activity {

    @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.main, menu);

        // Get the SearchView and set the searchable configuration
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.getItem(R.id.searchfield).getActionView();
        // Assumes current activity is the searchable activity
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        return true;
    }

}



SearchResultsActivity.java

package com.example.fidbacks_search;
import android.os.Bundle;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;

public class SearchResultsActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_results);
         Log.v("QUERY", "fuque");

        // 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);
          Log.v("QUERY", query.toString());
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.search_results, menu);
        return true;
    }

}



Searchable.xml

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



的AndroidManifest.xml

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

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.fidbacks_search.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>
        <activity
            android:name="com.example.fidbacks_search.SearchResultsActivity"
            android:label="@string/title_activity_search_results"
            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"/>
        </activity>
    </application>
</manifest>



布局/ activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <SearchView
        android:id="@+id/searchfield"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="144dp"
        android:iconifiedByDefault="false" >

    </SearchView>

</RelativeLayout>

3 个答案:

答案 0 :(得分:4)

在menu.xml中使用它: -

<item
    android:id="@+id/action_search"
    app:showAsAction="collapseActionView|always"
    android:title="@string/action_search"
    app:actionViewClass="android.support.v7.widget.SearchView"/>

所以你需要在菜单项标签中使用app namespace而不是android:

app:actionViewClass="android.support.v7.widget.SearchView"

答案 1 :(得分:3)

问题是因为SearchView UI位于 activity_main.xml 中,而在java文件中,您可以通过这种方式使用菜单项对象引用找到它的ID。

SearchView searchView = (SearchView) menu.getItem(R.id.searchfield).getActionView();

很明显你会得到Null指针异常。

因此,对于解决方案,您必须将其添加到菜单项中的main.xml文件中。

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

如果您在菜单项上不需要它,则无法添加 onCreateOptionsMenu 。你必须在oncreate()方法上使用它。但我认为搜索菜单将始终位于菜单项上。

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) findViewById(R.id.searchfield);
    // Assumes current activity is the searchable activity
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

答案 2 :(得分:1)

在调试中检查是否定义了menu.getItem(R.id.searchfield)。当您调用不存在元素的方法时,您将获得nullPoint异常。 另外,在调用方法之前,最好先检查元素是否存在。