Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.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 如何使用AsyncTask更新全局变量_Java_Android_Listview_Bitmap_Android Asynctask - Fatal编程技术网

Java 如何使用AsyncTask更新全局变量

Java 如何使用AsyncTask更新全局变量,java,android,listview,bitmap,android-asynctask,Java,Android,Listview,Bitmap,Android Asynctask,我要做的是:我使用一个自定义的“位置”适配器将行放入ListView。我试图在ListView的一行中添加一个位图。此位图来自URL。因此,我有一个全局变量public static Bitmap Bitmap,希望使用异步任务更新这个变量。这是我的密码: try { String s = ""; JSONArray jArray = new JSONArray(result);

我要做的是:我使用一个自定义的“位置”适配器将行放入
ListView
。我试图在ListView的一行中添加一个
位图。此位图来自URL。因此,我有一个全局变量
public static Bitmap Bitmap
,希望使用异步任务更新这个变量。这是我的密码:

            try {
                String s = "";
                JSONArray jArray = new JSONArray(result);

                for (int i = 0; i < jArray.length(); i++) {
                    final JSONObject json = jArray.getJSONObject(i);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                            try {

                                //here I am calling my new task and giving it the ID to find the image
                                BitmapWorkerTask myTask = new BitmapWorkerTask(json.getInt("ID"));
                                myTask.execute();
                                adapter.add(new Location(bitmap, json
                                        .getString("PlaceTitle"), json
                                        .getString("PlaceDetails"), json
                                        .getString("PlaceDistance"), json
                                        .getString("PlaceUpdatedTime")));

                                bitmap = null;
                            } catch (JSONException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                        }
                    });

                }

            } catch (Exception e) {
                // TODO: handle exception
                Log.e("log_tag", "Error Parsing Data " + e.toString());
            }
试试看{
字符串s=“”;
JSONArray jArray=新JSONArray(结果);
for(int i=0;i
这是我的任务

class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
    private int photoID = 0;

    public BitmapWorkerTask(int photoID) {
        // Use a WeakReference to ensure the ImageView can be garbage collected
        this.photoID = photoID;
    }

    // Decode image in background.
    @Override
    protected Bitmap doInBackground(Integer... params) {
        String initialURL = "http://afs.spotcontent.com/img/Places/Icons/";
        final String updatedURL = initialURL + photoID + ".jpg";
        Bitmap bitmap2 = null;

        try {
            bitmap2 = BitmapFactory.decodeStream((InputStream) new URL(
                    updatedURL).getContent());

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return bitmap2;
    }

    // Once complete, see if ImageView is still around and set bitmap.
    @Override
    protected void onPostExecute(Bitmap bitmap2) {
        bitmap = bitmap2;
    }
}
类BitmapWorkerTask扩展了AsyncTask{
私有int-photoID=0;
公共位图工作任务(int-photoID){
//使用WeakReference确保可以对ImageView进行垃圾收集
this.photoID=photoID;
}
//在背景中解码图像。
@凌驾
受保护位图doInBackground(整数…参数){
字符串initialURL=”http://afs.spotcontent.com/img/Places/Icons/";
最后一个字符串updateUrl=initialURL+photoID+“.jpg”;
位图bitmap2=null;
试一试{
bitmap2=BitmapFactory.decodeStream((InputStream)新URL(
getContent());
}捕获(格式错误){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
返回位图2;
}
//完成后,查看ImageView是否仍然存在并设置位图。
@凌驾
受保护的void onPostExecute(位图位图位图2){
位图=位图2;
}
}

因此,对于每次迭代,我都会向AsyncTask提供一个ID,它使用该ID查找图像,然后(我希望)应该更新传递到适配器的全局位图。当我运行我的应用程序时,每个列表的图片都是空的。有没有关于我做错了什么的想法?

考虑以下可能的命令执行顺序(记住任务在后台运行,因此顺序是不确定的):

  • myTask.execute()
  • BitmapWorkerTask.doInBackground()
  • 添加(新位置(位图…)
  • BitmapWorkerTask.onPostExecute()
  • 在步骤3中创建Location()对象时,传递的“bitmap”对象是全局对象指针。由于尚未调用onPostExecute(),因此其值无效。因此,Location对象是使用非位图对象创建的。在步骤4中,最终检索位图时,全局对象指针的值会更改(正确),但这不会影响已在步骤2传递到位置的(空)位图对象…这就是为什么在视图上看不到位图的原因


    可以做的是向BitmapWorkerTask构造函数传递一个附加参数:可以传递Location对象(或底层位图)然后,您可以使用检索到的位图更新该位置/位图对象。此处不需要全局变量。

    onPostExecute
    是否为
    bitmap2
    返回正确的位图?另外,
    onPostExecute
    中的
    位图
    变量不应该像在ClassName.bitmap中那样静态访问吗?我假设是这样正在为bitmap2发回正确的位图。doInBackground方法检索bitmap2,然后onPostExecute设置全局位图=bitmap2。正确吗?我知道在doInBackground中检索位图是成功的,因为我在AsyncTaskWell之外尝试过。让我们假设我们不知道。在doInBackground或ad上进行调试d
    System.out.println()
    到doInBackground中的异常,查看那里是否发生了任何事情。然后继续。正在检索位图。我设置了一个imageView=位图,它工作了。出于某种原因,它没有更新全局位图变量,尽管它工作了。非常感谢,我不知道为什么这个解决方案从我身边溜走了。+1!