为什么我的AutoCompleteTextView应用程序崩溃了

时间:2014-04-05 04:52:38

标签: java android autocomplete autocompletetextview

我正在尝试在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>

LogCat

3 个答案:

答案 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);

customlayoutTextView哪个ID为names的布局。