在Android片段上查看图像

在Android片段上查看图像,android,android-fragments,nullpointerexception,Android,Android Fragments,Nullpointerexception,我无法将可绘制文件夹中的图像显示到片段上。 当我尝试显示do it时,它会出现一个java.lang.NullPointerException。 任何帮助都将不胜感激 这是我的密码 public class Fragment4 extends Fragment { public Fragment4() { } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sav

我无法将可绘制文件夹中的图像显示到片段上。 当我尝试显示do it时,它会出现一个
java.lang.NullPointerException
。 任何帮助都将不胜感激

这是我的密码

public class Fragment4 extends Fragment {
    public Fragment4() {
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate (R.layout.fragment4, container, false);
        ImageView image = (ImageView)rootView.findViewById(R.id.image);
        image.setImageResource(R.drawable.image123);
        return  rootView;
    } 
}
XML文件

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

<ImageView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content" />

</LinearLayout>

这是因为您没有在xml中向图像添加id属性 试试这个:

<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

您的
ImageView
没有
id
。设定如下:

<ImageView
   android:id="@+id/image"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content" />


您可以添加布局xml文件吗?NullPointerException引用的行号是什么?(那个数字指的是哪一行)NullPointerException指的是这一行代码“image.setImageResource(R.drawable.image123);”