Andy Andy - 11 months ago 68
Android Question

Viewing an image on a Android Fragment

I am having trouble showing an image from the drawable folder on to a fragment.
When I try to show do it, it comes up with a

any help would be appreciated.

This is my code,

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(;
return rootView;

XML file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" >

android:layout_height="wrap_content" />


Answer Source

Your ImageView has no id. Set it as follows,

   android:layout_height="wrap_content" />