0

RealtiveLayoutでImageViewとtextviewを使用しています。相対レイアウトのOnclcik私はテキストビューの値を取得する必要があり、imageviewを変更する必要があります。NullPointer例外を取得しています。これがコードです

アクティビティコード

inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                int id=0;

                    for (int idx = 0; idx < dataFromDb.size(); idx++) {
                        id = idx + 1;
                        rel = (RelativeLayout) inflater.inflate(
                                R.layout.newdata, null);
                        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                                LayoutParams.WRAP_CONTENT,
                                LayoutParams.WRAP_CONTENT);
                        params.setMargins(0, 50, 0, 0);
                        txt = (TextView) rel.findViewById(R.id.fetchData);
                        txt.setText(dataFromDb.get(idx));
                        txt.setId(id);

                        img = (ImageView) rel.findViewById(R.id.tickbox);
                        img.setImageResource(R.drawable.tickbox1a);
                        img.setId(id);

                        rel.setOnClickListener(new ImageView.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                // TODO Auto-generated method stub
                                Toast.makeText(DoTask.this,
                                        "Selected" + v.getId(),
                                        Toast.LENGTH_LONG).show();
                                RelativeLayout im=(RelativeLayout)v;
                                ImageView i=(ImageView)im.findViewById(im);
                                i.setImageResource(R.drawable.tickbox1b);
                            }
                        });

                        content.addView(rel, params);

                    }

newData.xml

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

    <ImageView
        android:id="@+id/tickbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/tickbox1a"
        android:paddingBottom="10dp" />

    <TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/fetchData"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:paddingTop="2dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/gray"
        android:textSize="18sp" />

</RelativeLayout>

どんな助けでもいただければ幸いです。

4

2 に答える 2

5

私はあなたがそれがこのようであるべきだと信じています:

 public void onClick(View v) {
                            // TODO Auto-generated method stub
                            Toast.makeText(DoTask.this,
                                    "Selected" + v.getId(),
                                    Toast.LENGTH_LONG).show();
                            RelativeLayout im=(RelativeLayout)v;
                            ImageView i=(ImageView)im.findViewById(R.id.tickbox);
                            i.setImageResource(R.drawable.tickbox1b);
                        }

あなたはrelativelayoutでfindViewByIdを呼び出そうとしていますが、これは予期したときのビューであり、idです。または、上記で定義したimgを使用するだけで、imageviewの別のインスタンスを作成する必要はありません。これは、すでに1つあるためです。

于 2012-10-17T16:36:49.023 に答える
0

活動コードは

inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            int id=0;

                for (int idx = 0; idx < dataFromDb.size(); idx++) {
                    id = idx + 1;
                    rel = (RelativeLayout) inflater.inflate(
                            R.layout.newdata, null);
                    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                            LayoutParams.WRAP_CONTENT,
                            LayoutParams.WRAP_CONTENT);
                    params.setMargins(0, 50, 0, 0);
                    txt = (TextView) rel.findViewById(R.id.fetchData);
                    txt.setText(dataFromDb.get(idx));
                    txt.setId(id);

                    img = (ImageView) rel.findViewById(R.id.tickbox);
                    img.setImageResource(R.drawable.tickbox1a);
                    img.setId(id);

                    rel.setOnClickListener(new ImageView.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            Toast.makeText(DoTask.this,
                                    "Selected" + v.getId(),
                                    Toast.LENGTH_LONG).show();

                            img.setImageResource(R.drawable.tickbox1b);
                        }
                    });

                    content.addView(rel, params);

                }
于 2012-10-17T16:48:58.743 に答える