[英]Why is my application with AutoCompleteTextView crashing down
I am trying to learn AutoCompleteTextView in android. 我正在尝试在Android中学习AutoCompleteTextView。 I made a small app with AutoCompleteTextView against a few names. 我用AutoCompleteTextView针对几个名称制作了一个小型应用程序。 The app has no syntax errors. 该应用程序没有语法错误。 But, when I upload it to the emulator for testing, it crashes down at the start screen itself. 但是,当我将其上传到仿真器进行测试时,它在启动屏幕本身崩溃了。 Please help. 请帮忙。
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>
Change- 更改-
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.names);
Into- 进入
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
Also change- 还要更改-
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.id.names, presidents);
Into- 进入
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.select_dialog_item, presidents);
Change 更改
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.id.names,presidents);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
to 至
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);
Hope this will work 希望这会起作用
Here must check 这里必须检查
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.names);
that names
is the id of your AutoCompleteTextView
in your activity_main.xml
file. 该names
是您的activity_main.xml
文件中AutoCompleteTextView
的ID。
Because your logcat clearly says that you have a ClassCast Exception
. 因为您的logcat清楚地表明您有ClassCast Exception
。 So must check the id in your activity_main.xml
file. 因此必须检查您的activity_main.xml
文件中的ID。
So change here from 所以从这里改变
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.names);
to 至
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.id.names,presidents);
You need to apply your Threshold
to 1 instead of 3 because 您需要将Threshold
为1而不是3,因为
When threshold is less than or equals 0, a threshold of 1 is applied.
UPDATE: 更新:
You have to change this from 您必须将其更改为
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.id.names,presidents);
to 至
ArrayAdapter<String>p = new ArrayAdapter<String>(YourActivityName.this, R.layout.customlayout, R.id.names, presidents);
Where customlayout
is the layout in which your TextView
will be which id is names
. 其中customlayout
是您的TextView
的布局,其中id是names
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.