XML : Android Studio: SearchView: error in searchView.setSearchableInfo(searchManager.getSearchableInfo(cn));

I'm a newbie working on an android app, following is the first screen of the app. I want my application to perform search using that search dialog box.

enter image description here

For this, I followed http://developer.android.com/guide/topics/search/search-dialog.html

But I was getting following error: Logcat:

   FATAL EXCEPTION: main   Process: com.example.shalini.circlelayout, PID: 19330                                                    java.lang.NullPointerException   at com.example.shalini.circlelayout.MainActivity.onCreateOptionsMenu(MainActivity.java:69)    

where line:69 in MainActivity.java is:

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

I referred to this Cannot get searchview in actionbar to work and made changes but the same problem still persists. My question is why is it showing error in searchView.setSearchableInfo(searchManager.getSearchableInfo(cn)); line. I couldn't figure that out, I still think that error is arising due to some other part of the code, maybe searchresultsactivity.xml or SearchResultsActivity.java. Any help is much appreciated. Thank you. Code below:

MainActivity.java

  package com.example.shalini.circlelayout;    import android.app.SearchManager;  import android.content.ComponentName;  import android.content.Context;  import android.content.Intent;  import android.os.Bundle;  import android.support.design.widget.FloatingActionButton;  import android.support.design.widget.Snackbar;  import android.support.v7.app.AppCompatActivity;  import android.support.v7.widget.SearchView;  import android.support.v7.widget.Toolbar;  import android.view.MenuInflater;  import android.view.View;  import android.view.Menu;  import android.view.MenuItem;  import android.widget.EditText;  import android.widget.ImageButton;    public class MainActivity extends AppCompatActivity {      EditText e1;    ImageButton imageButton;        @Override      protected void onCreate(Bundle savedInstanceState) {            super.onCreate(savedInstanceState);          setContentView(R.layout.activity_main);          e1 = (EditText) findViewById(R.id.search);          addListenerOnButton();            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);          setSupportActionBar(toolbar);            FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);          fab.setOnClickListener(new View.OnClickListener() {              @Override              public void onClick(View view) {                  Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)                          .setAction("Action", null).show();              }          });      }    private void addListenerOnButton() {      imageButton = (ImageButton) findViewById(R.id.imageButton);      imageButton.setOnClickListener(new View.OnClickListener() {          @Override          public void onClick(View v) {              Intent i = new Intent(getApplicationContext(), SymptomsList.class);              startActivity(i);          }      });    }      @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);      MenuInflater inflater = getMenuInflater();      inflater.inflate(R.menu.menu_main, menu);      // Associate searchable configuration with the SearchView      SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);      SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();      ComponentName cn = new ComponentName(this, SearchResultsActivity.class);      searchView.setSearchableInfo(searchManager.getSearchableInfo(cn));      //call to getSearchableInfo()      //SearchableInfo object      //SearchView starts an activity with the ACTION_SEARCH intent when a user submits a query      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);  }  }    

SearchResultsActivity.java

  package com.example.shalini.circlelayout;    import android.app.ListActivity;  import android.app.SearchManager;  import android.content.Intent;  import android.os.Bundle;  import android.util.Log;    public class SearchResultsActivity extends ListActivity {      @Override    public void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);      handleIntent(getIntent());      }      @Override    public void onNewIntent(Intent intent) {      super.onNewIntent(intent);      handleIntent(intent);      }    private void handleIntent(Intent intent) {        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {          String query = intent.getStringExtra(SearchManager.QUERY);          doMySearch(query);          //use the query to search your data somehow      }    }    private void doMySearch(String query) {      Log.d("Event", query);    }  }    

content_main.xml

  <?xml version="1.0" encoding="utf-8"?>  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"  xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"  android:layout_height="wrap_content" android:padding="@dimen/activity_horizontal_margin"  app:layout_behavior="@string/appbar_scrolling_view_behavior"  tools:showIn="@layout/activity_main" tools:context=".MainActivity"  android:background="@drawable/blue_bluecrop1">    <EditText      android:layout_width="160dp"      android:layout_height="60dp"      android:id="@+id/search"      android:inputType="text"      android:hint="@string/sym"      android:drawableStart="@drawable/search_s2"      android:drawableLeft="@drawable/search_s2"        android:padding="20dp"      android:gravity="center_vertical|center_horizontal"      android:layout_alignParentTop="true"      android:layout_alignParentLeft="true"      android:layout_alignParentStart="true"      android:layout_alignParentRight="true"      android:layout_alignParentEnd="true"      android:layout_marginLeft="5dp"      android:layout_marginRight="5dp"      android:layout_marginTop="5dp"      android:ellipsize="end"      android:background="@drawable/roundededittext"/>    <ImageButton      android:layout_width="40dp"      android:layout_height="45dp"      android:contentDescription="@string/symbutton"      android:id="@+id/imageButton"      android:background="#00000000"      android:layout_marginLeft="160dp"      android:layout_marginStart="160dp"      android:layout_marginTop="270dp"      android:clickable="true"     />    

searchresultsactivity.xml

  <ListView  android:layout_width="match_parent"  android:layout_height="match_parent"  xmlns:android="http://schemas.android.com/apk/res/android">    

@xml/searchable

  <?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/search_searchable">    

AndroidManifest.xml

  <?xml version="1.0" encoding="utf-8"?>  <manifest xmlns:android="http://schemas.android.com/apk/res/android"  package="com.example.shalini.circlelayout" >    <application      android:allowBackup="true"      android:fullBackupContent="true"      android:icon="@mipmap/ic_launcher"      android:label="@string/app_name"      android:supportsRtl="true"      android:theme="@style/AppTheme" >      <activity          android:name=".MainActivity"            android:label="@string/app_name"          android:theme="@style/AppTheme.NoActionBar" >      <intent-filter>              <action android:name="android.intent.action.MAIN" />          <action android:name="android.intent.action.VIEW" />              <category android:name="android.intent.category.BROWSABLE"     />          <data android:scheme="http"              android:host="www.example.com"              android:pathPrefix="/doraemon" />        </intent-filter>          <meta-data              android:name="android.app.default_searchable"              android:value=".SearchResultsActivity" />          </activity>      <activity android:name=".SearchResultsActivity">          <intent-filter>              <action android:name="android.intent.action.SEARCH" />          </intent-filter>          <meta-data android:name="android.app.searchable"              android:resource="@xml/searchable" />      </activity>   </application>    

No comments:

Post a Comment