There is a menu:

<?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" xmlns:sabd="http://schemas.android.com/tools"> <item android:title="Change" app:showAsAction="always"></item> <item app:actionLayout="@layout/toolbar" android:title="" app:showAsAction="always"></item> <item android:icon="@android:drawable/ic_menu_search" android:title="" app:showAsAction="always" sabd:actionViewClass="android.support.v7.widget.SearchView" android:id="@+id/searchItem"></item> <item android:title=""> <item android:id="@+id/deleteAll" android:text="Delete All" android:title="Delete All"> </item> <item android:id="@+id/settings" android:icon="@android:drawable/ic_menu_preferences" android:title="Settings"> </item> </item> </menu> 

In this menu, I create an AutoCompleteTextView using the Toolbar :

 <?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" xmlns:android="http://schemas.android.com/apk/res/android"> <AutoCompleteTextView android:layout_width="133dp" android:layout_height="match_parent" android:id="@+id/myEditText" android:textColor="@color/Black"/> </android.support.v7.widget.Toolbar> 

How to learn text from AutoCompleteTextView ?

PS Tried to do this:

  @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.custom_menu,menu); AutoCompleteTextView search = (AutoCompleteTextView) findViewById(R.id.myEditText); ArrayAdapter adapt = new ArrayAdapter(this,android.R.layout.select_dialog_item,namespers); search.setAdapter(adapt); return super.onCreateOptionsMenu(menu); } 

There is an error:

 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.AutoCompleteTextView.setAdapter(android.widget.ListAdapter)' on a null object reference at com.example.x.myfirstnormal.FindSome.onCreateOptionsMenu(FindSome.java:193) 

    1 answer 1

     AutoCompleteTextView autoTextView = (AutoCompleteTextView) findViewById(R.id.myEditText); String str = autoTextView.getText().toString(); 

    Just checked to get the view inside the toolbar , you need to onCreate

      Toolbar toolbar = findViewById(R.id.my_toolbar); AutoCompleteTextView autoTextView = toolbar.findViewById(R.id.my_auto_text_view); 

    After that, you can access the autoTextView variable and you won't get null

    • 2
      java.lang.NullPointerException when using this method (the adapter is networked and an error occurs here). - True-hacker
    • 2
      How to fix the error? Drop the code where I call these methods? - True-hacker
    • one
      @ True-hacker Well, the error occurs, because not there look for a twist, you have it in the toolbar. Try first in onCreate to find the toolbar, and then, in the toolbar, in the same place onCreate, find AutoCompleteTextView, that is, so ... toolbar.findViewById (...) - Likhanov
    • I tried it like this: Toolbar toolbar = (Toolbar) findViewById (R.id.toolBar); AutoCompleteTextView search = toolbar.findViewById (R.id.myEditText); ArrayAdapter adapt = new ArrayAdapter (this, android.R.layout.select_dialog_item, namespers); search.setAdapter (adapt); It did not help - True-hacker
    • one
      Yes, it is, I don’t know if it’s possible to do something like this, connect a toolbar from another file. But if the toolbar is in the file with activation markup, there will be no error. You can include your file with toolbar in the xml file of activation using <include layout = "@ layout / name_toolbar_file" /> - Likhanov