我正在尝试在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>
答案 0 :(得分:3)
更改 -
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.names);
向 -
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
同时改变 -
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.id.names, presidents);
向 -
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.select_dialog_item, presidents);
答案 1 :(得分:1)
更改
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.id.names,presidents);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
到
AutoCompleteTextView textView=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
textView.setThreshold(1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,presidents);
textView.setAdapter(adapter);
希望这会起作用
答案 2 :(得分:0)
必须检查
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.names);
names
是您AutoCompleteTextView
文件中activity_main.xml
的ID。
因为您的logcat明确表示您拥有ClassCast Exception
。因此,必须检查activity_main.xml
文件中的ID。
从
改变AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.names);
到
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.id.names,presidents);
您需要将Threshold
应用于1而不是3,因为
When threshold is less than or equals 0, a threshold of 1 is applied.
<强>更新强>
您必须从
更改此设置ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.id.names,presidents);
到
ArrayAdapter<String>p = new ArrayAdapter<String>(YourActivityName.this, R.layout.customlayout, R.id.names, presidents);
customlayout
是TextView
哪个ID为names
的布局。