Need a TextView with autocompletion in AlertDialog .

Create a markup for AlertDialog

 <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <AutoCompleteTextView android:id="@+id/dialog_et_from" android:hint="@string/date_from" android:layout_width="match_parent" android:layout_height="150dp"/> </android.support.design.widget.TextInputLayout> <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <AutoCompleteTextView android:id="@+id/dialog_et_to" android:hint="@string/date_to" android:layout_width="match_parent" android:layout_height="150dp"/> </android.support.design.widget.TextInputLayout> 

I initialize the whole thing in the menu item:

  ArrayAdapter<String> adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity); List<String> labels = new ArrayList<>(); labels.add("text"); labels.add("test"); labels.add("abba"); adapter = new ArrayAdapter<>(getBaseContext(), android.R.layout.simple_list_item_1, labels); from = (AutoCompleteTextView) findViewById(R.id.dialog_et_from); to = (AutoCompleteTextView) findViewById(R.id.dialog_et_to); from.setAdapter(adapter); to.setAdapter(adapter); } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == android.R.id.home) { onBackPressed(); } if (id == R.id.action_choose_range) { AlertDialog.Builder builder = new AlertDialog.Builder(this); LayoutInflater inflater = getLayoutInflater(); View view = inflater.inflate(R.layout.dialog_view, null); builder.setView(view); builder.setTitle("Заголовок"); builder.show(); } return super.onOptionsItemSelected(item); } 

This log displays

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.AutoCompleteTextView.setAdapter(android.widget.ListAdapter)' on a null object reference at ru.whalemare.weather.activity.ChartActivity.onCreate(ChartActivity.java:139) 

Where am I wrong and how can I solve the problem?

    1 answer 1

    Probably something like that

     AlertDialog.Builder builder = new AlertDialog.Builder(this); ayoutInflater inflater = getLayoutInflater(); View view = inflater.inflate(R.layout.dialog_view, null); from = (AutoCompleteTextView) view.findViewById(R.id.dialog_et_from); to = (AutoCompleteTextView) view.findViewById(R.id.dialog_et_to); from.setAdapter(adapter); to.setAdapter(adapter); builder.setView(view); builder.setTitle("Заголовок"); builder.show(); } 
    • Exactly ... really stayed late. Thank you so much - whalemare