0

我已经定义了列表视图,并与布局 xml 文件中的列表视图的 ID 正确链接。我仍然收到上述错误。

我研究了这里发布的类似问题,但无济于事。

请帮忙。

public class ImageList extends ListActivity {

final static String imageUrls[] = { //some urls

        };

private List<Photo> model = new ArrayList<Photo>();
PhotoAdapter adapter = null;
ListView list = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.imagelist);

    list = (ListView) findViewById(R.id.lvImageList);
    // ArrayList<Bitmap> images = new ArrayList<Bitmap>();

    adapter = new PhotoAdapter(this, android.R.layout.simple_list_item_1,
            model);

    list.setAdapter(adapter);
    ProduceImageList(imageUrls);

}

public void ProduceImageList(String[] imageUrls) {
    int size = imageUrls.length;
    ImageView imagePlace = null;
    for (int i = 0; i < 4; i++) {
        imagePlace = (ImageView) getListView().getChildAt(i);
        downloadImage task = new downloadImage(imagePlace);
        task.execute(imageUrls[i]);
    }
}
 //asynchronous downloading of images
public class downloadImage extends AsyncTask<String, Void, Bitmap> {
    // private String url;
    Photo p = new Photo();
    private final WeakReference<ImageView> imageViewReference;

    public downloadImage(ImageView imageView) {
        imageViewReference = new WeakReference<ImageView>(imageView);
    }

    @Override
    protected Bitmap doInBackground(String... params) {
        // TODO Auto-generated method stub
        String localUrl = params[0];
        //here a bitmap download method is used which works fine separately
                    return BitmapDownload.DownloadBitmap(localUrl);
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        // TODO Auto-generated method stub
        if (isCancelled())
            result = null;
        if (imageViewReference != null) {
            ImageView imageView = imageViewReference.get();
            if (imageView != null) {
                p.setPhotoPic(result);
                adapter.add(p);
            }
        }

        super.onPostExecute(result);
    }

}
  //class defined with getter and setter method
public class Photo {
    private Bitmap pic = null;

    public Bitmap getPhotoPic() {
        return pic;
    }

    public void setPhotoPic(Bitmap incomingPic) {
        this.pic = incomingPic;
    }
}

 //custom ArrayAdapter to hold images
class PhotoAdapter extends ArrayAdapter<Photo> {

    public PhotoAdapter(Context context, int textViewResourceId,
            List<Photo> objects) {
        super(context, textViewResourceId, objects);
        // TODO Auto-generated constructor stub
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View row = convertView;
        PhotoHolder holder = null;
        if (row == null) {
            LayoutInflater inflater = getLayoutInflater();
            row = inflater.inflate(R.layout.imagelistrow, parent,false);
            holder = new PhotoHolder(row);
            row.setTag(holder);
        } else
            holder = (PhotoHolder) row.getTag();
        holder.populateFrom(model.get(position));
        return (row);
    }

}

 //holder for the row view
static class PhotoHolder {
    private ImageView imagePlace = null;
    private View row = null;

    PhotoHolder(View row) {
        this.row = row;

        imagePlace = (ImageView) row.findViewById(R.id.ivImageList);
    }

    void populateFrom(Photo p) {

        imagePlace.setImageBitmap(p.getPhotoPic());
    }
}

}

我的看法是...

    <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="schemas.android.com/apk/res/android"; android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> 
         <ListView android:id="@+id/lvImageList" android:layout_width="match_parent" android:layout_height="wrap_content"/>
    </LinearLayout>
4

2 回答 2

3

您不能使用自定义 ListView ID,ListActivity同时ListActivity,根据错误,您的ListView. 目前,您使用的自定义 ID 为lvImageList. 要么更改您的 Activity 以使用Activity和不使用,ListActivity要么更新您的 ListView 的 XML 以使用:

<ListView
    android:id="@android:id/android:list"
    ... />
于 2013-04-02T23:23:34.993 回答
0

无需在 xml 文件中定义自定义 Listview id。您只需使用 android Listview id,无需在ListActivity中按 id 查找视图,只需调用getListView()即可获取在 Layout 文件中使用默认 android id 定义的 listview

于 2013-04-03T04:41:58.673 回答