DJD*_*ave 1 android casting view
我是Android开发的新手,并且一直在尝试使用初学者的教程作为开发简单应用程序的起点.有一个屏幕,一个图像,一排四个按钮,一个供用户输入图钉的文本框,以及一个显示结果的文本视图.
<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>
Run Code Online (Sandbox Code Playgroud)
我有代码根据按下的按钮提供我想要的结果 - 一切都很好.但后来我希望用户输入PIN以及按下按钮,所以我添加了EditText控件.这引发了以下错误:
E/AndroidRuntime(28578): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
Run Code Online (Sandbox Code Playgroud)
我的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);
}
Run Code Online (Sandbox Code Playgroud)
我以为我做了一些愚蠢的事情,使用TextView的名称而不是EditText控件,但如果我有,我找不到它.
该错误正在线上引发
getPage("thisweek");
Run Code Online (Sandbox Code Playgroud)
我不明白这一行如何涉及任何类型的视图,当然还有函数标题
this_week_click(View view)
Run Code Online (Sandbox Code Playgroud)
当我更改XML文件中的TextView和EditText的顺序(以便TextView首先出现)时,错误消失了.就好像传递的"视图"不是按钮,而是按钮的最近的小部件.我读过了
但它似乎只能证实我(错误)理解按钮应作为视图参数传递.我也尝试过清理项目,并建立一个全新的项目.究竟是什么导致了铸造错误?
| 归档时间: |
|
| 查看次数: |
109 次 |
| 最近记录: |