2012-06-06 47 views
0

在一個TabHost中,我設置了一個TabSpec,它包含一個帶有幾個按鈕和下面的一個ListView的標題區域。管理此TabSpec的活動被定義爲「擴展ListActivity」。 但是,現在我遇到了無法定義OnClickListener來檢查按下提交按鈕的問題。我該如何解決這個問題?TabSpec與ListActivity ...如何設置Button的OnClickListener?

試圖通過

btnRatingSubmit.setOnClickListener((OnClickListener) this); 

引發一個ClassCastException異常鑄按鈕...

這裏是佈局的基本摘錄:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<RatingBar 
    android:id="@+id/ratingBar" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

<EditText 
    android:id="@+id/edComment" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:inputType="textMultiLine" > 
</EditText> 

<Button 
    android:id="@+id/btnSubmit" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/btnSubmit" /> 

<ListView 
    android:id="@android:id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" /> 

</LinearLayout> 

回答

1

而不是

btnRatingSubmit.setOnClickListener((OnClickListener) this); 

試試這個:

btnRatingSubmit.setOnClickListener(new View.OnClickListener(){ 

public void onClick(View v) { 
      // TODO Auto-generated method stub 

     } 
    }); 
+0

謝謝 - 此解決方案爲我工作! – richey

1

它不應該是一個問題,如果setContentView(R.layout.your_layout);並獲得btnSubmit的實例。但是,如果您遇到問題,請將ListActivity更改爲Activity

0

我認爲你的問題是((OnClickListener)這個)。如果沒有更多的這門課程的代碼,我無法完全說出什麼是錯的。但是,你是否在持有這個按鈕的Activity上實現了OnClickListener?否則,請嘗試像這樣轉換((View.OnClickListener)this)。

0

您應該實現View.OnClickListener的活動繼續使用(OnClickListener)這一點,或者乾脆創建一個新的View.OnClickListener取代「這個」