Adding image programmatically to RelativeLayout

浪子不回头ぞ 提交于 2019-12-05 02:44:36

问题


I want to add to a linearlayout, various relative layouts via code. Each relative layouts consists of: an imageview at the left, a textview beside it to the right (exactly in the middle) and another Image at the right. I must add them using data read from a database. It must be with relativelayout because I want to use a listener over the image and a different listener over the RL.

Each relativelayout looks like this thing I have in 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>

I am using this code: but I get a NullPointerException at the moments of making the addView() for the image, I tried just with adding the textView and it works.

    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();
 }

回答1:


you are using (ImageView)findViewById(R.drawable.animalplanet) and its not a proper way to programatically do it afaik. If you want to inflate an ImageView you should do something like this:

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);    

However, if you will only display the image without beeing clickable and loading it from a database I suggest you use a ViewBinder




回答2:


You don't make a new image, thats why you get nullpointer exception. You should either make a new or layout inflate a new.



来源:https://stackoverflow.com/questions/10435689/adding-image-programmatically-to-relativelayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!