Java AutoCompleteTextView无法像EditText那样工作

Java AutoCompleteTextView无法像EditText那样工作,java,android,autocomplete,autocompletetextview,Java,Android,Autocomplete,Autocompletetextview,在我的应用程序中,我用AutoCompleteTextView替换了EditText,使其更加友好,但我有一个错误 在应用程序中,用户输入一个植物的名称,然后单击一个按钮进入一个新的活动,其中显示了有关该植物的一些信息 错误:在用户输入工厂名称后,我按下按钮进入下一个活动后,应用程序崩溃。当我还有一个编辑文本时,这种情况就没有发生。也许我使用AutoCompleteTextView时出错了? 以下是相关代码: public class Main2Activity extends AppCompa

在我的应用程序中,我用AutoCompleteTextView替换了EditText,使其更加友好,但我有一个错误

在应用程序中,用户输入一个植物的名称,然后单击一个按钮进入一个新的活动,其中显示了有关该植物的一些信息

错误:在用户输入工厂名称后,我按下按钮进入下一个活动后,应用程序崩溃。当我还有一个编辑文本时,这种情况就没有发生。也许我使用AutoCompleteTextView时出错了?

以下是相关代码:

public class Main2Activity extends AppCompatActivity {

AutoCompleteTextView edit;

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

    //this is the AutoComplete
    AutoCompleteTextView edit = (AutoCompleteTextView) findViewById(R.id.et_item);

    //this is the list of suggestions for the AutoComplete
    String[] items = getResources().getStringArray(R.array.items_array);
    java.util.Arrays.sort(items);
    ArrayAdapter<String> adapter =
            new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
    edit.setAdapter(adapter);

//this is the method that is called when the button is pressed
public void find(View view) {

    String name = edit.getText().toString();

    //basically, whatever is typed into the AutoComplete is turned into a string, and 
    //if the string matches one of the existing plants, the user is taken to 
    //the next activity

    if(name.equalsIgnoreCase("Sunflower")){
        Intent intent = new Intent(this, Sunflower.class);
        startActivity(intent);
    }
    else if(name.equalsIgnoreCase("Cactus")){
        Intent intent = new Intent(this, Cactus.class);
        startActivity(intent);
    }
公共类Main2活动扩展了AppCompative活动{
自动完成文本视图编辑;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
//这是自动完成
AutoCompleteTextView编辑=(AutoCompleteTextView)findViewById(R.id.et_项);
//这是自动完成的建议列表
String[]items=getResources().getStringArray(R.array.items\u数组);
java.util.Arrays.sort(items);
阵列适配器=
新的ArrayAdapter(这是android.R.layout.simple_list_item_1,items);
edit.setAdapter(适配器);
//这是按下按钮时调用的方法
公共void查找(视图){
字符串名称=edit.getText().toString();
//基本上,在自动完成中键入的任何内容都会转换为字符串
//如果字符串与现有工厂之一匹配,则用户将被带到
//下一个活动
if(名称:equalsIgnoreCase(“Sunflower”)){
意向意向=新意向(此,Sunflower.class);
星触觉(意向);
}
else if(name.equalsIgnoreCase(“仙人掌”)){
意向意向=新意向(这是Cactus.class);
星触觉(意向);
}
有人知道为什么这不起作用吗?

更改如下:

 AutoCompleteTextView edit = (AutoCompleteTextView) findViewById(R.id.et_item);
致:

您的自动完成文本视图范围仅限于
onCreate()

edit.getText().toString()

您正试图从中获取尚未初始化的文本。因此它将获得空指针异常。

更改如下:

 AutoCompleteTextView edit = (AutoCompleteTextView) findViewById(R.id.et_item);
致:

您的自动完成文本视图范围仅限于
onCreate()

edit.getText().toString()


您正试图从中获取尚未初始化的文本。因此它将出现空指针异常。

您帮了大忙!谢谢!很高兴它帮了大忙。您帮了大忙!谢谢!很高兴它帮了大忙。