0

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

//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 新手。谢谢你。

4

3 回答 3

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]);
于 2013-08-16T05:07:06.163 回答
1

尝试以下代码链接:-

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;

}

}

于 2013-08-16T05:07:01.587 回答
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);
于 2013-08-16T05:06:41.200 回答