Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java android延迟加载库问题_Java_Android - Fatal编程技术网

Java android延迟加载库问题

Java android延迟加载库问题,java,android,Java,Android,我正在尝试在asynctask中异步下载一些图像,然后在onPostExecute()中将图像设置为库适配器。我已经硬编码了要在asyntask中加载的图像,因为我甚至无法到达将图像传递给适配器构造函数的部分:/ 不会抛出任何错误,postexecute Log()不会向logcat发布任何内容 有人能告诉我哪里出了问题吗 公共类GallerythreadedtestActivity扩展活动{ private Gallery gallery; private AddImgAdp adapter;

我正在尝试在asynctask中异步下载一些图像,然后在onPostExecute()中将图像设置为库适配器。我已经硬编码了要在asyntask中加载的图像,因为我甚至无法到达将图像传递给适配器构造函数的部分:/ 不会抛出任何错误,postexecute Log()不会向logcat发布任何内容

有人能告诉我哪里出了问题吗

公共类GallerythreadedtestActivity扩展活动{

private Gallery gallery;
private AddImgAdp adapter;
private Drawable[] image;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    new downloadImages().execute();
}

public class AddImgAdp extends BaseAdapter {
    int GalItemBg;
    private Context cont;

    // Adding images.
    private Integer[] Imgid = {
    R.drawable.icon, R.drawable.icon, R.drawable.icon, R.drawable.icon
    };

    public AddImgAdp(Context c) {
    cont = c;
    TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
    GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
    typArray.recycle();
    }

    public int getCount() {
    return Imgid.length;
    }

    public Object getItem(int position) {
    return position;
    }

    public long getItemId(int position) {
    return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imgView = new ImageView(cont);

    imgView.setImageDrawable(image[position]);
    // Fixing width & height for image to display
    imgView.setLayoutParams(new Gallery.LayoutParams(80, 70));
    imgView.setScaleType(ImageView.ScaleType.FIT_XY);
    imgView.setBackgroundResource(GalItemBg);

    return imgView;
    }
    }

public class downloadImages extends AsyncTask<String, Void, Void> {

    protected void onPreExecute(){
        image=new Drawable[2];
        adapter = new AddImgAdp(getApplicationContext());
        gallery = (Gallery) findViewById(R.id.examplegallery);
    }

    protected void onProgressUpdate(Void... v){

    }

    protected Void doInBackground(final String... args) {


        image[0] = ImageOperations(getApplicationContext(),"http://t3.gstatic.com/images?q=tbn:ANd9GcQidl6KX2jRWNeCA6jT_TjWG7NlI3aRiB_AcDsA9Y5owS2cr9G6","image.jpg");
        image[1] = ImageOperations(getApplicationContext(),"http://t3.gstatic.com/images?q=tbn:ANd9GcQidl6KX2jRWNeCA6jT_TjWG7NlI3aRiB_AcDsA9Y5owS2cr9G6","image.jpg");

        return null;

    }

    protected void onPostExecute(){
        Log.d("onpost","");
        gallery.setAdapter(adapter);
        gallery.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v, int position, long id) {
            Toast.makeText(GallerythreadedtestActivity.this, "Position=" + position, Toast.LENGTH_SHORT).show();
            }
            });
    }

}

private Drawable ImageOperations(Context ctx, String url, String saveFilename) {
    try {
        InputStream is = (InputStream) this.fetch(url);
        Drawable d = Drawable.createFromStream(is, "src");
        return d;
    } catch (MalformedURLException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

public Object fetch(String address) throws MalformedURLException,IOException {
    URL url = new URL(address);
    Object content = url.getContent();
    return content;
}
私人画廊;
专用AddImgAdp适配器;
私有可绘制图像;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
新建下载图像().execute();
}
公共类addimgapp扩展BaseAdapter{
int GalItemBg;
私人语境控制;
//添加图像。
私有整数[]Imgid={
R.drawable.icon,R.drawable.icon,R.drawable.icon,R.drawable.icon
};
公共AddImgAdp(上下文c){
cont=c;
TypedArray typArray=获取StyledAttributes(R.styleable.GalleryTheme);
GalItemBg=typArray.getResourceId(R.styleable.GalleryTheme\u android\u galleryItemBackground,0);
typArray.recycle();
}
public int getCount(){
返回Imgid.length;
}
公共对象getItem(int位置){
返回位置;
}
公共长getItemId(int位置){
返回位置;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
ImageView imgView=新的ImageView(续);
imgView.setImageDrawable(图像[位置]);
//固定要显示的图像的宽度和高度
imgView.setLayoutParams(新图库.LayoutParams(80,70));
imgView.setScaleType(ImageView.ScaleType.FIT_XY);
imgView.setBackgroundResource(GalItemBg);
返回imgView;
}
}
公共类下载任务{
受保护的void onPreExecute(){
图像=新的可绘制[2];
adapter=newaddimgapp(getApplicationContext());
gallery=(gallery)findViewById(R.id.examplegallery);
}
受保护的void onProgressUpdate(void…v){
}
受保护的Void doInBackground(最终字符串…args){
图像[0]=图像操作(getApplicationContext(),”http://t3.gstatic.com/images?q=tbn:ANd9GcQidl6KX2jRWNeCA6jT_TjWG7NlI3aRiB_AcDsA9Y5owS2cr9G6“,”image.jpg“;
图像[1]=图像操作(getApplicationContext(),”http://t3.gstatic.com/images?q=tbn:ANd9GcQidl6KX2jRWNeCA6jT_TjWG7NlI3aRiB_AcDsA9Y5owS2cr9G6“,”image.jpg“;
返回null;
}
受保护的void onPostExecute(){
Log.d(“onpost”和“);
gallery.setAdapter(适配器);
gallery.setOnItemClickListener(新的OnItemClickListener(){
public void onItemClick(AdapterView父视图、视图v、整型位置、长id){
Toast.makeText(GallerythreadedtestActivity.this,“Position=“+Position,Toast.LENGTH_SHORT).show();
}
});
}
}
私有可绘制图像操作(上下文ctx、字符串url、字符串保存文件名){
试一试{
InputStream为=(InputStream)this.fetch(url);
Drawable d=Drawable.createFromStream(是“src”);
返回d;
}捕获(格式错误){
e、 printStackTrace();
返回null;
}捕获(IOE异常){
e、 printStackTrace();
返回null;
}
}
公共对象获取(字符串地址)引发畸形DurLexException,IOException{
URL=新的URL(地址);
Object content=url.getContent();
返回内容;
}

}

我可以给你一个建议,请尝试这也是一个补充解决方案,最好延迟加载图像:。如果您将xml中的列表视图元素更改为Gallery视图,我认为这将解决您的问题