我是新來的android開發,並一直試圖使用初學者的教程作爲我的出發點開發一個簡單的應用程序。有一個屏幕,一個圖像,一排四個按鈕,供用戶輸入針的文本框以及顯示結果的文本視圖。在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>
我有一些代碼來提供基於按下按鈕,我想要的結果 - 所有的罰款。但之後我希望用戶輸入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");
我不明白這條線如何參與意見任何形式的,當然功能標題
this_week_click(View view)
確實,當我改變了XML文件中的TextView和EditText的順序(以便TextView最先),錯誤消失了。就好像傳遞的「視圖」不是按鈕,而是最靠近按鈕的部件。我已閱讀
existence of parameter (View view)
但它只是似乎證實了我的(MIS)的理解是,一個按鈕應該作爲視圖參數傳遞。我也嘗試清理項目,並建立一個全新的項目。究竟是什麼導致了鑄造錯誤?
哪條線做它崩潰? – Niklas
不初始化任何功能的任何東西,在創建初始化 – appukrb
清潔您的項目 – Nfear