0

I would like to create a Search button on android, and set the default text. So I am using the setQuery method

My activity_main.xml

<SearchView
                android:id="@+id/searchview"
                android:layout_width="210dp"
                android:layout_height="40dp"
                android:layout_marginLeft="15dp"
                android:iconifiedByDefault="false"
                android:textSize="14sp"
                android:focusable="false"/>

MainActivity.java

SearchView searchView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        searchView = findViewById(R.id.searchview);
        searchView.setQuery("Search Here",false);
    }

I get the following error message:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.aaa.system/com.aaa.system.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SearchView.setQuery(java.lang.CharSequence, boolean)' on a null object reference

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SearchView.setQuery(java.lang.CharSequence, boolean)' on a null object reference at com.aaa.system.MainActivity.onCreate(MainActivity.java

Any idea what is causing this error?

Draken
  • 3,109
  • 13
  • 34
  • 52
JessciaLau
  • 49
  • 5
  • 1
    Missing a `setContentView`: https://stackoverflow.com/q/24706348/2711811 . –  Nov 02 '21 at 08:36

0 Answers0