質問

I am trying to learn AutoCompleteTextView in android. I made a small app with AutoCompleteTextView against a few names. 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>

LogCat

役に立ちましたか?

解決

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.

Because your logcat clearly says that you have a ClassCast Exception. So must check the id in your activity_main.xmlfile.

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

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top