应用程序实现AutoCompleteTextView在启动时崩溃

时间:2014-04-05 17:53:27

标签: java android classcastexception autocompletetextview

我正在使用AutoCompleteTextView的应用程序,并根据所选的选项在Activities之间切换。但是,现在我的应用程序在启动时崩溃了。我不明白为什么。请帮忙。

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Toast;
import android.view.inputmethod.InputMethodManager;


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

    AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.select_dialog_item, presidents);

    textView.setThreshold(3);
    textView.setAdapter(adapter);
    textView.setOnItemSelectedListener((OnItemSelectedListener) this);
    textView.setOnItemClickListener((OnItemClickListener) this);
    textView.setOnItemSelectedListener(new OnItemSelectedListener()
    {   
    @Override
    public void onItemSelected(AdapterView<?> parent, View view,
                   int index, long id)
    {
        int position=0;
        if(position == 1){
            Intent i = new Intent (MainActivity.this, SecondActivity.class);
            i.putExtra("KEY", presidents[index]);
            startActivity(i);
            }
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }
   });

    }
}

LogCat

布局:

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

2 个答案:

答案 0 :(得分:2)

textView.setOnItemSelectedListener((OnItemSelectedListener) this);是罪魁祸首。

编辑: 这一个textView.setOnItemClickListener((OnItemClickListener) this);  导致ClassCastException

如果你熟悉JAVA,你可能知道为什么会发生这种异常,如果不是你,只需点击几下即可找到大惊小怪。

为什么会发生这种情况

上述行意味着课程this会覆盖OnItemSelectedListenerOnItemClickListener。在你的情况下,Activity类。

PS:我认为还需要纠正以下几行

int position=0; //You assign 0 to the variable
if(position == 1){ // And compare it to 1
    Intent i = new Intent (MainActivity.this, SecondActivity.class);
    i.putExtra("KEY", presidents[index]);
    startActivity(i);
}

答案 1 :(得分:2)

在这些行中,您有ClassCastException

textView.setOnItemSelectedListener((OnItemSelectedListener) this);
textView.setOnItemClickListener((OnItemClickListener) this);

您正在尝试将此(活动)强制转换为侦听器实例。方法setOnItemSelectedListener()setOnItemClickListener()将侦听器实例作为参数传递,因此您无法将引用传递给您的活动。以下是MainActivity的代码:

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

    AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.select_dialog_item, presidents);

    textView.setThreshold(3);
    textView.setAdapter(adapter);
        textView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
       // your logic here
    }
    });
    textView.setOnItemSelectedListener(new OnItemSelectedListener()
    {   
    @Override
    public void onItemSelected(AdapterView<?> parent, View view,
           int index, long id)
    {
    int position=0;
    if(position == 1){
        //Intent i = new Intent (MainActivity.this, SecondActivity.class);
        //i.putExtra("KEY", presidents[index]);
        //startActivity(i);
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

    }
    });
    }
}

当然你应该为cklicks实现逻辑。