列表空指针异常中的ImageView

时间:2013-08-16 05:01:54

标签: java android android-listview

我正在尝试设置仅包含一些图像的列表视图,即没有文本。这是我的文件:

//the Main File:
    public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView listview1 = (ListView)findViewById(R.id.listView1);
        final Integer[] mThumbIds = {
                R.drawable.blue, R.drawable.blue2, 
                R.drawable.blue3, R.drawable.blue4,
                R.drawable.blue5, R.drawable.blue6
        };
        listview1.setAdapter(new ImageAdapter(this,mThumbIds));
        listview1.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
               //Upon Clicking
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    class ImageAdapter extends ArrayAdapter<Integer> {
        private Context mContext;
        Integer [] resources;
        public ImageAdapter (Context c, Integer[] resources) {
            super(c, R.layout.activity_main, resources);
            //System.out.println("Set up of Image Adapter");
            mContext = c;
            this.resources=resources;
            for(Integer resource:resources){
                System.out.println(resource);
            }
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View rowView = inflater.inflate(R.layout.activity_main, parent, false);
            ImageView imageView = (ImageView) rowView.findViewById(R.layout.image_view);
            imageView.setImageResource(resources[position]);
            return rowView;
        }    
    }
   }

XML如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/export_folder_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Formation Name:"
        android:textAppearance="?android:attr/textAppearanceMedium" />
    <ListView
        android:id="@+id/listView1"        
        android:layout_below="@+id/color_textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:columnWidth="90dp"
        android:numColumns="auto_fit"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:stretchMode="columnWidth"
        android:gravity="center"
        android:background="#ff000000" >
     </ListView>
</RelativeLayout>

最后,定义ImageView的那个:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:minHeight="64dp">
    <ImageView
            android:id="@+id/imageView"
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="9dp"
            android:layout_alignParentTop="true"/>
</RelativeLayout>

不幸的是,我在图像视图上设置图像资源的位置获得了NullPointerExceptions。图像资源存在 - 我已经确定了。 我不确定我做错了什么,而且我是Android新手。谢谢。

3 个答案:

答案 0 :(得分:2)

看起来你在getView(),

中正在冒一个错误的xml
View rowView = inflater.inflate(R.layout.activity_main, parent, false);

此处R.layout.activity_main应替换为包含ImageView的xml文件。

从您的代码中,我认为包含ImageView的xml文件的名称为R.layout.image_view

所以,你的最终代码应该是这样的,

View rowView = inflater.inflate(R.layout.image_view, parent, false);
            ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView);
            imageView.setImageResource(resources[position]);

答案 1 :(得分:1)

请尝试以下代码link: -

public View getView(int position,View convertView,ViewGroup parent){

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(rowResourceId, parent, false);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView);


    int id = Integer.parseInt(Ids[position]);
    String imageFile = Model.GetbyId(id).IconFile;


    // get input stream
    InputStream ims = null;
    try {
        ims = context.getAssets().open(imageFile);
    } catch (IOException e) {
        e.printStackTrace();
    }
    // load image as Drawable
    Drawable d = Drawable.createFromStream(ims, null);
    // set image to ImageView
    imageView.setImageDrawable(d);
    return rowView;

}

}

答案 2 :(得分:0)

使用R.id.image_view代替R.layout.image_view 更改行

 ImageView imageView = (ImageView) rowView.findViewById(R.layout.image_view);

到下线

 ImageView imageView = (ImageView) rowView.findViewById(R.id.image_view);