-1

クリック可能になるようにlinearlayerを設定し、ボタンのように機能して新しいアクティビティを開始したいと考えています。しかし、私はエラーが発生しました。ここに.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

「bProduct1 = (Button) findViewById(R.id.llproduct1);」のクラスキャストが間違っています。

「llproduct1」は LinearLayout です!! ボタンではありません。
したがって、Java コードは ClassCastException を引き起こします。

onClick メソッドは View クラスで宣言されています。
LinearLayout と Button の両方が View クラスを継承しています。

それでは、以下のコードを修正してみませんか。

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