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

问题描述 投票:1回答:2

我正在开发一个使用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

    }
   });

    }
}

布局:

<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>
java android classcastexception autocompletetextview
2个回答
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);
}

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

    }
    });
    }
}

当然,您应该实现点击的逻辑。

© www.soinside.com 2019 - 2024. All rights reserved.