Change View from other thread

半城伤御伤魂 提交于 2019-12-02 11:02:51

问题


I wrote a code to download an image from internet. And i have to show it in a ImageView which is dynamically created.

And i am getting an error that Only the original thread that created a view hierarchy can touch its views. I know i have to write a handle but how can i do that?

Here is my code:

public class ResimCek implements Runnable {

        int resimID = 0;

        public ResimCek(int parcaID) {
            // store parameter for later user
            resimID = parcaID;
        }

        public void run() {

            int resID = getResources().getIdentifier(Integer.toString(resimID) , "tag", getPackageName()); 
            ImageView resim = (ImageView) findViewById(resID);

            Drawable image = ImageOperations(getBaseContext(),"http://141.11.11.206/parca/" + Integer.toString(resimID) + ".jpg" ,"I" + Integer.toString(resimID) + ".jpg");

            // I AM GETTING ERROR HERE ******************
            resim.setImageDrawable(image); // *************************
        }
    }

    private Drawable ImageOperations(Context ctx, String url, String saveFilename) {
        try {
            InputStream is = (InputStream) this.fetch(url);
            Drawable d = Drawable.createFromStream(new URL(url).openConnection().getInputStream(),saveFilename);
            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;
    }
    private void MalzemeEkle(String malzemeKodu, String malzemeTanimi) {
        ImageView parcaresmi = new ImageView(this);
        parcaresmi.setId(Integer.parseInt(malzemeKodu));
        Runnable r = new ResimCek(Integer.parseInt(malzemeKodu)); 
        new Thread(r).start(); 
}

回答1:


public class ResimCek implements Runnable {

        int resimID = 0;

        public ResimCek(int parcaID) {
            // store parameter for later user
            resimID = parcaID;
        }

        public void run() {

            int resID = getResources().getIdentifier(Integer.toString(resimID) , "tag", getPackageName()); 
            ImageView resim = (ImageView) findViewById(resID);

            Drawable image = ImageOperations(getBaseContext(),"http://141.11.11.206/parca/" + Integer.toString(resimID) + ".jpg" ,"I" + Integer.toString(resimID) + ".jpg");

            // I AM GETTING ERROR HERE ******************
            resim.setImageDrawable(image); // *************************
        }
    }



new Handler().post(new ResimCek(Integer.parseInt(malzemeKodu))); instead of new Thread(r).start(); 

by any case if this is an Activity.. then

runOnUIThread(new ResimCek(Integer.parseInt(malzemeKodu))); `instead of new Thread(r).start();` 

will also work..




回答2:


You should create a handler :

    final Handler   myHandler   = new Handler() {
                                    @Override
                                    public void handleMessage(Message msg)
                                    {
                                        /*do all your ui action here to display the image ()*/
                                        resim.setImageDrawable(image);
                                    }
                                };

And in your runnable when the image is downloaded call :

myHandler.sendEmptyMessage(0);

There are other options for handler you can find here http://developer.android.com/reference/android/os/Handler.html



来源:https://stackoverflow.com/questions/10209819/change-view-from-other-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!