Android 在布局中添加图像

Android 在布局中添加图像,android,image,imageview,Android,Image,Imageview,我想在现有布局中添加图像。我使用以下代码段创建图像: 此类使用正确的图像创建ImageView。我想在此布局中添加图像: <?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=

我想在现有布局中添加图像。我使用以下代码段创建图像:

此类使用正确的图像创建ImageView。我想在此布局中添加图像:

<?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>
但我只得到一个“NullPointerException”

有什么建议该做什么或什么是不正确的吗? 非常感谢


编辑:我的意思是“查看linearLayout=findViewById(R.id.iv_objekt);”

在尝试引用linearLayout之后,您正在执行
setContentView
。这样做:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.objekt_einzeln); 

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

}
但即使这样,您也引用了一个
线性布局
,它不在您提供的XML中


确保首先执行
setContentView
,然后确保
LinearLayout
实际位于您在
setContentView
中提供的布局中,只需调用setContentView(R.layout.objekt_einzeln);在开始时,然后设置图像视图

指定的布局中不存在ID
R.ID.ll_picture_和_reflection
。你的意思是
R.id.ll\u缩略图


在尝试引用布局中的任何元素之前,您还需要使用
setContentView

findViewById()
的任何指令之前,应该先使用
setContentView()。我编辑了我的帖子,意思是“查看linearLayout=findViewById(R.id.iv_objekt);”
。我尝试了您的解决方案,但仍然出现错误:
您必须调用removeView()
。啊,它工作,完美!非常感谢。
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.objekt_einzeln); 

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

}