android - 在 Android 应用程序中按下按钮会产生转换错误

标签 android view casting

我是 android 开发的新手,一直在尝试使用初学者教程作为我开发简单应用程序的起点。有一个屏幕,其中有一个图像、一排四个按钮、一个供用户输入 pin 的文本框和一个用于显示结果的 TextView 。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linear_layout_1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
       android:id="@+id/imageView1"
       android:contentDescription="@string/image"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
        android:src="@drawable/skytrek"
       />

    <LinearLayout
    android:id="@+id/linear_layout_2"
        android:layout_width="wrap_content"
       android:layout_height="wrap_content"
        android:orientation="horizontal" >

                <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:text="@string/button_today"
        android:onClick="today_click" />

            <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:text="@string/button_tomorrow"
        android:onClick="tomorrow_click" /> 

            <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:text="@string/button_this_week"
        android:onClick="this_week_click" /> 

          <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:text="@string/button_next_week"
        android:onClick="next_week_click" />
    </LinearLayout>

        <EditText android:id="@+id/editText1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:hint="@string/edit_message" />

    <TextView
       android:id="@+id/textView1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@string/init_message"/>


</LinearLayout>

我有代码可以根据按下的按钮提供我想要的结果 - 一切正常。但后来我希望用户输入 PIN 以及按下按钮,所以我添加了 EditText 控件。这引发了以下错误:

E/AndroidRuntime(28578): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText

我的 Java 类:

private String content = null;  
private TextView textView1;
private EditText editText1;

public void today_click(View view) {
    getPage("today");
}
public void tomorrow_click(View view) {
    getPage("tomorrow");
}
public void this_week_click(View view) {
    getPage("thisweek");
}
public void next_week_click(View view) {
    getPage("nextweek");
}

public void getPage(String strParam) {

    editText1 = (EditText) findViewById(R.id.editText1);
    String message = editText1.getText().toString();

    if (message.equals("4567")) {

                content = "PIN recognised";
    } else {
        content = "PIN not recognised";
    }
    textView1 = (TextView) findViewById(R.id.textView1);
    textView1.setText(content);
}

我以为我做了一些愚蠢的事情,使用了 TextView 的名称而不是 EditText 控件,但即使我有也找不到。

该行抛出错误

getPage("thisweek");

我不明白这一行如何涉及任何类型的 View ,但当然是函数标题

this_week_click(View view)

确实如此,当我更改 XML 文件中 TextView 和 EditText 的顺序(以便 TextView 先出现)时,错误消失了。好像传递的“ View ”不是按钮,而是距离按钮最近的小部件。我读过

existence of parameter (View view)

但这似乎只是证实了我的(错误的)理解,即按钮应该作为 View 参数传递。我还尝试清理项目,并构建一个全新的项目。究竟是什么导致了类型转换错误?

最佳答案

如果您使用的是 Eclipse,请转到菜单语音“项目”并选择“清理”

有时 Eclipse 的 id 有一些问题,通过清理你重新生成它们的项目..

关于android - 在 Android 应用程序中按下按钮会产生转换错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21016437/

相关文章:

android - 选择选项卡时如何为选项卡布局中的项目设置动画?

android - 获取Android Linux内核的内核命令行参数

膨胀 View 时Android ListView NullPointerException

Android - notifyDataSetChanged() 和线程

sql - View 有什么用?

MySQL 使用 select * from tablename definition 查看性能

python - 如何为python自定义类进行类型转换

C++ 模板和以基类和子类作为模板参数的转换

c - 将 void * 指针变量从 unsigned int 类型转换为 uint32 时出现编译时错误

android - 来自 HTC EVO 3D 的深度图