如何自动完成TextView到对话框

时间:2015-07-05 15:09:21

标签: android autocompletetextview

我的android程序有问题,我需要将AutoCompleteTextView放入Dialog但不能正常工作。

我将分享我的代码:

public class RegisterActivity extends AppCompatActivity {
private ExpandableListView myList;
private List<Convenios> convenioList;
private ArrayList<String> convenioNames = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register); }

public void CadastrarNovoConvenio(View view) {
    CadastraConvenio();
}
private void CadastraConvenio()  {
    final Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.add_new_convenio);

    final RadioGroup tipoConvenio = (RadioGroup) dialog.findViewById(R.id.rbg);
    final Button save = (Button) dialog.findViewById(R.id.bt_cadastrar);
    final Button cancel = (Button) dialog.findViewById(R.id.bt_cancelar);

    final AutoCompleteTextView convenioTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextViewConvenio);

    dialog.setTitle("Add new Item:");

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, convenioNames);
    convenioTextView.setAdapter(adapter);
    convenioTextView.setThreshold(1);

    //exibe na tela o dialog
    dialog.show();
  }

此代码行中出现问题:

 convenioTextView.setAdapter(adapter);
 convenioTextView.setThreshold(1);

1 个答案:

答案 0 :(得分:0)

您正在从活动视图请求AutocompliteTextView。 但它在对话视图中。因此,您在autocompliteTextView变量中为null,并在convenioTextView.setAdapter(适配器)上获取NullPointerException。

您需要更改此代码:

    final AutoCompleteTextView convenioTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextViewConvenio)

对此:

    final AutoCompleteTextView convenioTextView = (AutoCompleteTextView) dialog.getView().findViewById(R.id.autoCompleteTextViewConvenio);