4

線形レイアウトに、コードを介してさまざまな相対レイアウトを追加したいと思います。それぞれの相対的なレイアウトは、左側のimageview、右側の横にあるtextview(ちょうど真ん中)、および右側の別のImageで構成されています。データベースから読み取ったデータを使用して追加する必要があります。イメージではリスナーを使用し、RLでは別のリスナーを使用するため、relativelayoutを使用する必要があります。

各relativelayoutは、私がXMLで持っているもののように見えます。

<RelativeLayout
    android:id="@+id/relativecanal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

       <ImageView
           android:id="@+id/imageView1"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignParentLeft="true"
           android:layout_alignParentTop="true"
           android:src="@drawable/antena3" />

       <TextView
           android:id="@+id/textView1"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_centerVertical="true"
           android:layout_marginLeft="39dp"
           android:layout_toRightOf="@+id/imageView1"
           android:text="Large Text"
           android:textAppearance="?android:attr/textAppearanceLarge" />

       <ImageView
           android:id="@+id/imageView2"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignParentRight="true"
           android:layout_centerVertical="true"
           android:src="@drawable/star" />

    </RelativeLayout>

私はこのコードを使用しています:しかし、画像のaddView()を作成する瞬間にNullPointerExceptionが発生します。textViewを追加するだけで動作します。

    try {
     File sdcard = Environment.getExternalStorageDirectory();
     File file = new File(sdcard,archivoCanales);
  LinearLayout layout = (LinearLayout) findViewById(R.id.channelslistlayout);
     BufferedReader br = new BufferedReader(new FileReader(file));
     String line;



     while ((line = br.readLine()) != null) {



         RelativeLayout channel=new RelativeLayout(this);
         channel.setBackgroundColor(2);

         TextView channelName=new TextView(this);
         channelName.setText(new String(line));

         ImageView image=(ImageView)findViewById(R.drawable.animalplanet);


      LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
              LinearLayout.LayoutParams.MATCH_PARENT,
              LinearLayout.LayoutParams.WRAP_CONTENT
      );    

    RelativeLayout.LayoutParams firstImageParams = new RelativeLayout.LayoutParams(
            android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
            android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
            firstImageParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);


         RelativeLayout.LayoutParams secImageParams = new RelativeLayout.LayoutParams(
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
                secImageParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);


         channel.addView(image,firstImageParams);
         channel.addView(channelName,secImageParams);

         layout.addView(channel,llp);
}

 }
 catch (IOException e) {
    e.printStackTrace();
 }
4

2 に答える 2

6

あなたが使用(ImageView)findViewById(R.drawable.animalplanet)しているのですが、プログラムでそれを行うには適切な方法ではありません。あなたが膨らませたいならば、ImageViewあなたはこのような何かをするべきです:

RelativeLayout.LayoutParams imParams = 
new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
ImageView imSex = new ImageView(context);
imSex.setImageResource(getmyImage());
imSex.setId(2);
addView(imSex,imParams);    

ただし、クリック可能でデータベースからロードせずに画像のみを表示する場合は、ViewBinderを使用することをお勧めします

于 2012-05-03T17:14:04.113 に答える
4

新しいイメージを作成しないため、nullpointer例外が発生します。新しいものを作成するか、レイアウトを新しくする必要があります。

于 2012-05-03T16:53:10.920 に答える