【问题标题】:Why is my application with AutoCompleteTextView crashing down为什么我的带有 AutoCompleteTextView 的应用程序崩溃了
【发布时间】:2014-05-17 14:08:51
【问题描述】:

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

【问题讨论】:

  • 请发布您的activity_main.xml

标签: java android autocomplete autocompletetextview


【解决方案1】:

这里一定要检查

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

namesAutoCompleteTextView 在您的 activity_main.xml 文件中的 ID。

因为您的 logcat 清楚地表明您有一个ClassCast Exception。所以必须检查你的activity_main.xmlfile 中的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

【讨论】:

  • AutoCompleteTextView 的 id 是 'autoCompleteTextView1' & textView 的 id 是 'names'
  • 我已经改了。现在应用程序启动。但是,当我输入 3 个字母作为 textView.setThreshold(3);应用程序崩溃。
  • 我将其更改为 1,现在应用程序在我输入第一个字母时立即崩溃。
  • 是的。但是,我无法获得您的自定义布局。
  • 那么你的TextView在哪个xml文件中??
【解决方案2】:

改变-

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

【讨论】:

  • 我已经改了。现在应用程序启动。但是,当我输入 3 个字母作为 textView.setThreshold(3);应用程序崩溃。
  • 您在 ArrayAdapter 中使用文本视图 ID 作为参数。这是错误的。
  • android 提供了一些内置布局供使用,例如 android.R.layout.select_dialog_item。
  • 好的。知道了。我可以再问一件事吗?
  • 看,当我输入一些东西时,我会得到一个自动完成的预测。就像当我输入“Joh”时,我得到“John F. Kennedy”。现在,如果用户点击“John F. Kennedy”,它会将他带入一个活动,该活动向他提供有关 John F. Kennedy 的信息。我应该怎么做才能得到这个?如何使自动完成预测可点击?
【解决方案3】:

改变

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

希望这会奏效

【讨论】:

  • 我做了,但是当我输入第一个字母时它崩溃了。
  • 发布您的 logcat 和 xml 文件。
  • 更新了 logcat 和 XML。
  • 说。如果可以的话。我一定会帮忙的。
  • 看,当我输入一些东西时,我会得到一个自动完成的预测。就像当我输入“Joh”时,我得到“John F. Kennedy”。现在,如果用户单击“John F. Kennedy”,它会将他带入一个活动,该活动向他提供有关 John F. Kennedy 的信息。我应该怎么做才能得到这个?如何使自动完成预测可点击?
猜你喜欢
  • 1970-01-01
  • 2014-09-20
  • 2010-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多