Solution 1 :
This is not allowed in android. You are only allowed to find a view that you can see in the UI. Try using the binding for the SearchView as your content.
Example:
binding = SearchViewBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
Problem :
I want to include a SearchView I have found onto my Android app, but I am facing an issue when setAdapter binds the adapter to the view:
OnCreate from MainActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
// Locate the ListView in listview_main.xml
list = findViewById(R.id.listview);
for (int i = 0; i < animalNameList.length; i++) {
AnimalNames animalNames = new AnimalNames(animalNameList[i]);
// Binds all strings into an array
arraylist.add(animalNames);
}
// Pass results to ListViewAdapter Class
adapter = new ListViewAdapter(this, arraylist);
// Binds the Adapter to the ListView
list.setAdapter(adapter);
// Locate the EditText in listview_main.xml
editsearch = findViewById(R.id.search);
editsearch.setOnQueryTextListener(this);
setSupportActionBar(binding.appBarMain.toolbar);
DrawerLayout drawer = binding.drawerLayout;
NavigationView navigationView = binding.navView;
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(R.id.nav_main,R.id.nav_add_alumno,
R.id.nav_alumnado, R.id.nav_ejercicios, R.id.nav_insignias, R.id.nav_settings)
.setOpenableLayout(drawer)
.build();
NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment_content_main);
NavController navController = navHostFragment.getNavController();
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
Logcat with error:
2022-06-13 19:13:12.720 9197-9197/com.example.llm E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.llm, PID: 9197
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.llm/com.example.llm.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.example.llm.MainActivity.onCreate(MainActivity.java:63)
at android.app.Activity.performCreate(Activity.java:7994)
at android.app.Activity.performCreate(Activity.java:7978)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
I have done some research but every page does the same procedure: take the view from .xml, take the data to an ArrayList or some similar object, create the adapter and do a .setAdapter.
I don’t really know if this would be enough information, if it isn’t I can add everything needed.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns_android="http://schemas.android.com/apk/res/android"
xmlns_app="http://schemas.android.com/apk/res-auto"
xmlns_tools="http://schemas.android.com/tools"
android_id="@+id/drawer_layout"
android_layout_width="match_parent"
android_layout_height="match_parent"
android_fitsSystemWindows="true"
tools_openDrawer="start">
<include
android_id="@+id/app_bar_main"
layout="@layout/app_bar_main"
android_layout_width="match_parent"
android_layout_height="match_parent" />
<com.google.android.material.navigation.NavigationView
android_id="@+id/nav_view"
android_layout_width="wrap_content"
android_layout_height="match_parent"
android_layout_gravity="start"
android_fitsSystemWindows="true"
app_headerLayout="@layout/nav_header_main"
app_menu="@menu/activity_main_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>
By the way, the view where the SearchView is used is the following:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns_android="http://schemas.android.com/apk/res/android"
xmlns_app="http://schemas.android.com/apk/res-auto"
xmlns_tools="http://schemas.android.com/tools"
android_layout_width="match_parent"
android_layout_height="match_parent">
<ImageView
android_id="@+id/imLogo"
android_layout_width="250dp"
android_layout_height="100dp"
android_layout_marginTop="36dp"
android_src="@drawable/logo"
app_layout_constraintEnd_toEndOf="parent"
app_layout_constraintStart_toStartOf="parent"
app_layout_constraintTop_toTopOf="parent" />
<TextView
android_id="@+id/txtNombre"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_text="Grupo: "
android_textSize="16sp"
app_layout_constraintBottom_toBottomOf="@+id/spinner"
app_layout_constraintEnd_toStartOf="@+id/spinner"
app_layout_constraintHorizontal_bias="0.917"
app_layout_constraintStart_toStartOf="parent"
app_layout_constraintTop_toTopOf="@+id/spinner"
app_layout_constraintVertical_bias="0.5" />
<Spinner
android_id="@+id/spinner"
android_layout_width="210dp"
android_layout_height="48dp"
android_contentDescription="@string/spinnerRef"
app_layout_constraintBottom_toTopOf="@+id/txtDescription"
app_layout_constraintEnd_toEndOf="parent"
app_layout_constraintHorizontal_bias="0.5"
app_layout_constraintStart_toEndOf="@+id/txtNombre"
app_layout_constraintStart_toStartOf="parent"
app_layout_constraintTop_toBottomOf="@+id/imLogo" />
<TextView
android_id="@+id/txtDescription"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_marginBottom="16dp"
android_text="Búsqueda de alumnos: "
android_textSize="16sp"
app_layout_constraintBottom_toTopOf="@+id/relativeLayout3"
app_layout_constraintEnd_toEndOf="parent"
app_layout_constraintHorizontal_bias="0.172"
app_layout_constraintStart_toStartOf="parent" />
<RelativeLayout
android_id="@+id/relativeLayout3"
android_layout_width="613dp"
android_layout_height="521dp"
app_layout_constraintBottom_toBottomOf="parent"
app_layout_constraintEnd_toEndOf="parent"
app_layout_constraintStart_toStartOf="parent"
app_layout_constraintTop_toBottomOf="@+id/imLogo"
app_layout_constraintVertical_bias="0.38">
<SearchView
android_id="@+id/search"
android_layout_width="fill_parent"
android_layout_height="wrap_content"
android_iconifiedByDefault="false">
<requestFocus />
</SearchView>
<ListView
android_id="@+id/listview"
android_layout_width="fill_parent"
android_layout_height="fill_parent"
android_layout_below="@+id/search" />
</RelativeLayout>
<Button
android_id="@+id/btnCrearCurso"
android_layout_width="350dp"
android_layout_height="60dp"
android_backgroundTint="@color/blueFlag"
android_text="Añadir Estudiantes"
app_cornerRadius="20dp"
app_iconPadding="0dp"
app_layout_constraintBottom_toBottomOf="parent"
app_layout_constraintEnd_toEndOf="parent"
app_layout_constraintStart_toStartOf="parent"
app_layout_constraintTop_toBottomOf="@+id/relativeLayout3" />
</androidx.constraintlayout.widget.ConstraintLayout>
Comments
Comment posted by Jian Astrero
Can you please show the contents of activity_main.xml
Comment posted by Alberto Díaz
I also included the view where the SearchView is used