0

I am working on an application in which i am setting SearchView widget on the action bar. But i am always getting null pointer exception . I am not getting any idea why it's happening. I followed Many SO post but none of them are helping.

Please guide me if i am making some mistake... The snippets are

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>

These are all the codes which i am using for search thing. But i am not able to resolve the Null Pointer exception getting at this line in OnCreateOptions(menu)

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

anand
  • 1,711
  • 2
  • 24
  • 59

0 Answers0