在操作栏的searchview中获取NullPointerException

时间:2014-11-11 08:01:14

标签: android android-actionbar android-search

我正在开发一个应用程序,我在操作栏上设置了SearchView小部件。但我总是得到空指针异常。我不知道为什么会这样。我跟着许多SO帖子,但没有一个人在帮忙。

如果我犯了一些错误,请指导我......这些片段是

SearchActivity.java

public class SearchActivity extends ActionBarActivity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_Search);
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);
        handleIntent(getIntent());
    }

   private void handleIntent(Intent intent) {
        if(Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            doSearch(query);
        }else if(Intent.ACTION_VIEW.equals(intent.getAction())){
            getPlace(intent.getStringExtra(SearchManager.EXTRA_DATA_KEY));
        }
    }

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        MenuItem searchItem = menu.findItem(R.id.action_search);

        // Get the SearchView and set the searchable configuration
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); // At this line i am getting the Null Pointer exception
        searchView.setIconifiedByDefault(false);  
        return super.onCreateOptionsMenu(menu);
    }    
}

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="Settings"
    android:searchSettingsDescription="Settings" 
    android:searchSuggestAuthority="com.stata.mobile.android.dao.PlaceProvider"
    android:searchSuggestIntentAction="android.intent.action.VIEW"    
    android:searchSuggestSelection=" ?"
    android:searchSuggestThreshold="2" >
</searchable>

的AndroidManifest.xml

        <activity
            android:name=".ui.SearchActivity"
            android:label=""
            android:launchMode="singleTop">
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>
            <!-- Points to searchable activity -->
            <meta-data android:name="android.app.default_searchable"
                android:value=".ui.SearchActivity" />
            <!-- Points to searchable meta data -->
            <meta-data android:name="android.app.searchable"
                android:resource="@xml/searchable"/>
        </activity>

这些是我用于搜索的所有代码。但我无法解决在OnCreateOptions(菜单)中获取此行的空指针异常

searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

0 个答案:

没有答案