Java 为什么我的带有AutoCompleteTextView的应用程序会崩溃

Java 为什么我的带有AutoCompleteTextView的应用程序会崩溃,java,android,autocomplete,autocompletetextview,Java,Android,Autocomplete,Autocompletetextview,我正在尝试学习android中的AutoCompleteTextView。我用AutoCompleteTextView制作了一个小应用程序,与几个名字进行比较。应用程序没有语法错误。但是,当我将它上传到模拟器进行测试时,它会在开始屏幕上崩溃。 请帮忙 package com.mavenmaverick.autocomplete_test; import android.os.Bundle; import android.app.Activity; import android.view.Men

我正在尝试学习android中的AutoCompleteTextView。我用AutoCompleteTextView制作了一个小应用程序,与几个名字进行比较。应用程序没有语法错误。但是,当我将它上传到模拟器进行测试时,它会在开始屏幕上崩溃。 请帮忙

package com.mavenmaverick.autocomplete_test;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;


public class MainActivity extends Activity {

String[] presidents= { "John F. Kennedy",
                    "Lyndon B. Johnson",
                    "Richard Nixon",
                    "Gerald Ford",
                    "Jimmy Carter",
                    "Ronald Reagan",
                    "George H. W. Bush",
                    "Bill Clinton",
                    "George W. Bush",
                    "Barack Obama"
                    };

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

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.id.names,presidents);
    AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);

    textView.setThreshold(3);
    textView.setAdapter(adapter);

}

}





<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<AutoCompleteTextView
    android:id="@+id/autoCompleteTextView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/names"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:ems="10" >

    <requestFocus />
</AutoCompleteTextView>

<TextView
    android:id="@+id/names"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="38dp"
    android:text="@string/hello_world" />

</RelativeLayout>
package com.mavenmaverick.autocomplete\u测试;
导入android.os.Bundle;
导入android.app.Activity;
导入android.view.Menu;
导入android.widget.ArrayAdapter;
导入android.widget.AutoCompleteTextView;
公共类MainActivity扩展了活动{
字符串[]总统={“约翰·F·肯尼迪”,
“林登·B·约翰逊”,
“理查德·尼克松”,
“杰拉尔德·福特”,
“吉米·卡特”,
“罗纳德·里根”,
“乔治·H·W·布什”,
“比尔·克林顿”,
“乔治·W·布什”,
“巴拉克·奥巴马”
};
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter=新的ArrayAdapter(这个,R.id.names,总统);
AutoCompleteTextView textView=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
设置阈值(3);
setAdapter(适配器);
}
}
这里必须检查

AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.names);
names
activity\u main.xml
文件中的
AutoCompleteTextView
的id

因为您的logcat清楚地表明您有一个
ClassCast异常
。因此,您必须检查
activity\u main.xml
文件中的id

所以从这里改变

AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.names);

更新:

你必须把这个从

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.id.names,presidents);
ArrayAdapter adapter=新的ArrayAdapter(这个,R.id.names,总统);

ArrayAdapterp=newarrayadapter(YourActivityName.this、R.layout.customlayout、R.id.names、presidents);
其中,
customlayout
是您的
TextView
将使用的布局,id是
名称

,此处必须检查

AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.names);
names
activity\u main.xml
文件中的
AutoCompleteTextView
的id

因为您的logcat清楚地表明您有一个
ClassCast异常
。因此,您必须检查
activity\u main.xml
文件中的id

所以从这里改变

AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.names);

更新:

你必须把这个从

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.id.names,presidents);
ArrayAdapter adapter=新的ArrayAdapter(这个,R.id.names,总统);

ArrayAdapterp=newarrayadapter(YourActivityName.this、R.layout.customlayout、R.id.names、presidents);
其中,
customlayout
是您的
TextView
将使用的布局,id是
名称

更改-

AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.names);
进入-

也改变-

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.id.names, presidents);
ArrayAdapter adapter=新的ArrayAdapter(这个,R.id.names,总统);
进入-

ArrayAdapter adapter=新的ArrayAdapter(这是android.R.layout.select_对话框_项,总统);
更改-

AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.names);
进入-

也改变-

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.id.names, presidents);
ArrayAdapter adapter=新的ArrayAdapter(这个,R.id.names,总统);
进入-

ArrayAdapter adapter=新的ArrayAdapter(这是android.R.layout.select_对话框_项,总统);
更改

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.id.names,presidents);
    AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
ArrayAdapter adapter=新的ArrayAdapter(这个,R.id.names,总统);
AutoCompleteTextView textView=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);

autocompletextview textView=(autocompletextview)findViewById(R.id.autocompletextview 1);
设置阈值(1);
ArrayAdapter=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,总统);
setAdapter(适配器);
希望这能起作用

改变

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.id.names,presidents);
    AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
ArrayAdapter adapter=新的ArrayAdapter(这个,R.id.names,总统);
AutoCompleteTextView textView=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);

autocompletextview textView=(autocompletextview)findViewById(R.id.autocompletextview 1);
设置阈值(1);
ArrayAdapter=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,总统);
setAdapter(适配器);


希望这能起作用

发布您的活动_main.xml,请发布您的活动_main.xml,请将AutoCompleteTextView的id设置为'autoCompleteTextView1',textView的id设置为'names',我已对其进行了更改。现在应用程序启动了。但是,当我输入3个字母作为textView.setThreshold(3);应用程序崩溃。我将其更改为1,现在只要输入第一个字母,应用程序就会崩溃。是的。但是,我无法获取您的自定义布局。因此,您的TextView在哪里,其中xml文件??AutoCompleteTextView的id为'autoCompleteTextView1',TextView的id为'names',我已经更改了它。现在应用程序启动了。但是,当我输入3个字母作为textView.setThreshold(3);应用程序崩溃。我将其更改为1,现在只要输入第一个字母,应用程序就会崩溃。是的。但是,我无法获取您的自定义布局。那么,您的TextView在哪一个xml文件中?我已经更改了它。现在应用程序启动了。但是,当我输入3个字母作为textView.setThreshold(3);应用程序崩溃。您正在使用文本视图id作为ArrayAdapter中的参数。这是错误的。android提供了一些内置的布局来使用,比如android.R.layout.select\u dialog\u item.ok。知道了。我可以问另一件事吗?看,当我输入一些东西时,我得到一个自动完成的预测。就像当我输入“约翰”时,我得到的是“约翰·F·肯尼迪”。现在,如果用户点击“约翰·F·肯尼迪”,他就会进入一个活动,向他提供关于约翰·F·肯尼迪的信息。我该怎么做才能得到这个?如何使“自动完成预测”可单击?我已更改了它。现在应用程序启动了。但是,当我输入3个字母作为textView.setThreshold(3)时