-1

我已将线性层设置为可点击,并希望它像一个按钮一样开始一个新的活动。但是我得到了错误。这是 .xml 的一部分

            <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 = (Button) findViewById(R.id.llproduct1); bProduct1.setOnClickListener(new View.OnClickListener() {

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

什么地方出了错?

4

2 回答 2

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

您不能将 LinearLayout 转换为按钮。但你可以这样做:

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

请参阅此以供参考

于 2011-10-18T14:07:23.740 回答
0

“bProduct1 = (Button) findViewById(R.id.llproduct1);”中的类转换错误

'llproduct1' 是线性布局!!不是按钮。
因此java代码导致ClassCastException。

onClick 方法在 View 类中声明。
LinearLayout 和 Button 都继承了 View 类。

所以你为什么不修复下面的代码。

View bProduct1 = findViewById(R.id.llproduct1);
bProduct1.setOnClickListener(......);
于 2011-10-18T14:09:46.943 回答