2014-01-09 37 views
1

我是新來的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)的理解是,一個按鈕應該作爲視圖參數傳遞。我也嘗試清理項目,並建立一個全新的項目。究竟是什麼導致了鑄造錯誤?

+0

哪條線做它崩潰? – Niklas

+0

不初始化任何功能的任何東西,在創建初始化 – appukrb

+0

清潔您的項目 – Nfear

回答

5

如果你正在使用Eclipse,進入菜單聲音「項目」,並通過清理你生成它們的項目中選擇「清除」

有時候Eclipse有一些問題,IDS,..

+0

+1對你的預測 –

+0

我說過我曾試過清洗 - 但我沒有意識到我需要做*每次*我改變了XML!謝謝。 – DJDave

1

你好我測試了你的代碼,你的代碼很好。請清理您的項目

選擇您的項目,然後點擊項目,然後點擊清理,同時檢查自動生成。您的自動生成的類R沒有正確生成。

2

每次,如果您在xml中進行任何更改或在xml中更改任何視圖位置或更改了id,則需要清理構建項目。否則您將得到此異常。

0

嘗試清潔您的項目,然後再次運行它.. 的Eclipse - > Project菜單 - >清潔

這是因爲日食的時候我們在XML文件中玩耍變得混亂;)

相關問題