2011-10-18 48 views
0

我已將線性圖層設置爲可點擊,並希望它像按鈕一樣操作並開始新的活動。但是我得到錯誤。下面是將.xmlAndroid可點擊圖層

  <LinearLayout 
      android:id="@+id/llproduct1" 
      android:layout_width="fill_parent" android:layout_height="wrap_content" 
      android:orientation="vertical" android:clickable="true"> 
      <ImageView .... /> 
      <TextView .... /> 
      </LinearLayout> 

這是的.java 按鈕bProduct1 =(按鈕)findViewById(R.id.llproduct1)的一部分; bProduct1.setOnClickListener(新View.OnClickListener(){

 @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      startActivity(new Intent("com.testing.PRODUCTDESCRIPTION")); 
     } 

出了什麼問題

+0

什麼是你的錯誤? –

+1

ClassCastException顯然 – Blackbelt

回答

0
Button bProduct1 = (Button) findViewById(R.id.llproduct1); 

你不能投你的LinearLayout到一個按鈕,但你可以這麼做:?

LinearLayout bProduct1 = (LinearLayout) findViewById(R.id.llproduct1); 
bProduct1.setOnClickListener(...) 

see this for reference

+0

謝謝!但我想我還有一個錯誤,仍然有錯誤。 <活動機器人:名稱= 「產品描述。」 機器人:標籤= 「@串/ APP_NAME」> <意圖濾波器> <操作機器人:名稱= 「com.testing.PRODUCTDESCRIPTION」/> <類別機器人:name =「android.intent.category.DEFAULT」/> user996481

+0

已解決。謝謝!!! – user996481

0

這是錯誤的類鑄造在「bProd uct1 =(Button)findViewById(R.id.llproduct1); 「

'llproduct1' 是的LinearLayout !!不是一個按鈕。
因此java代碼原因ClassCastException異常。

的onClick方法在View類聲明。
和二者的LinearLayout和按鈕被繼承View類。

那麼你爲什麼不解決下面的代碼。

View bProduct1 = findViewById(R.id.llproduct1); 
bProduct1.setOnClickListener(......); 
+0

謝謝!但我想我還有一個錯誤,仍然有錯誤。 user996481

+0

已解決。謝謝!!! – user996481