I am making an android app in which i added a menu file where i added an item to perform search. Then on the MainActivity i added a searchView variable which i am trying to inflate in the onCreatOptionsmenu()
method. When i try to run the app i get this error, "java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SearchView.setSearchableInfo(android.app.SearchableInfo)' on a null object reference"
Here is the code to my menu main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:title="@string/search"
android:orderInCategory="1"
app:showAsAction ="ifRoom"
android:id="@+id/action_search"
/>
</menu>
MainActivity code for searchview
public class MainActivity extends AppCompatActivity {
private SearchView searchView;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.action_search)
.getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setMaxWidth(Integer.MAX_VALUE);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
mCorona_Stats_Adapter.getFilter().filter(query);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
mCorona_Stats_Adapter.getFilter().filter(newText);
return false;
}
});
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int menuItemThatWasSelected = item.getItemId();
if(menuItemThatWasSelected == R.id.action_search){
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
if (!searchView.isIconified()) {
searchView.setIconified(true);
return;
}
super.onBackPressed();
}
}
build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.passengerearth.coronago"
minSdkVersion 15
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
AndroidX replaces the original support library APIs with packages in the androidx namespace.
You are using androidx
. So add androidx support.
<item
android:id="@+id/action_search"
.....................
app:actionViewClass="androidx.appcompat.widget.SearchView" />
And import below in MainActivity section.
import androidx.appcompat.widget.SearchView;