I want to add an Image in an existing Layout. I create the Image with this code snippet:
http://www.inter-fuser.com/2009/12/android-reflections-with-bitmaps.html
This class creates a ImageView with the right image. I want to add the Image in this layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<include
android:id="@+id/actionbar_back"
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="@layout/actionbar_back" />
<LinearLayout
android:id="@+id/ll_ueberschrift"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/gray"
android:gravity="center"
android:layout_below="@id/actionbar_back" >
<TextView
android:id="@+id/tv_ueberschrift"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Java"
android:layout_gravity="right"
android:textSize="30sp" />
</LinearLayout>
<!-- ListRow Left sied Thumbnail image -->
<LinearLayout
android:id="@+id/ll_thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dip"
android:layout_marginTop="20dp"
android:layout_below="@id/ll_ueberschrift" >
<ImageView
android:id="@+id/iv_objekt"
android:contentDescription="@string/description"
android:layout_width="120sp"
android:layout_height="120sp"
android:paddingLeft="5dp" />
</LinearLayout>
<RelativeLayout>
Now I try to add the Image to the existing ImageView:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
//Create an Image view and add our bitmap with reflection to it
ImageView imageView = new ImageView(this);
imageView.setImageBitmap(bitmapWithReflection);
imageView.setPadding(5, 5, 5, 5);
View linearLayout = findViewById(R.id.ll_picture_and_reflection);
((LinearLayout) linearLayout).addView(imageView);
setContentView(R.layout.objekt_einzeln);
}
But I just a get “NullPointerException”.
Any suggestion what to do or what is incorrect?
Thank a lot
EDIT: I meant “View linearLayout = findViewById(R.id.iv_objekt);”
You are doing
setContentViewafter you are trying to reference the LinearLayout. Do this:But even then you are referencing a
LinearLayoutwhich is not in that XML you provided.Make sure you do
setContentViewfirst, then make sure theLinearLayoutis actually in the layout you provide insetContentView