roiberg
roiberg

Reputation: 13747

How to refresh a url and load a new image

I'm trying to use timer to refresh an image from the internet.

This is my code:

public class ProjectActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    BannerActivity ba = new BannerActivity(this);
    LinearLayout layout = (LinearLayout)findViewById(R.id.main_layout);
    layout.addView(ba);
}

public class BannerActivity extends ImageButton implements OnClickListener{
    URL url = null;
    public BannerActivity(Context context) {
        super(context);
        setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 300));
        loadimage();
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                loadimage();
            }   
            }, 5000, 1000);
    }

    private void loadimage(){
        try {
            url = new URL("http://3.bp.blogspot.com/_9UYLMDqrnnE/S4UgSrTt8LI/AAAAAAAADxI/drlWsmQ8HW0/s400/sachin_tendulkar_double_century.jpg");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        InputStream content = null;
        try {
            content = (InputStream)url.getContent();
        } catch (IOException e) {
            e.printStackTrace();
        }
        final Drawable d = Drawable.createFromStream(content , "src"); 
        setBackgroundDrawable(d);

        setOnClickListener(this);
    }

The error that I'm getting is this:

CalledFromWrongThreadException: Only the original thread that created a view
hierarchy can touch its views.

I'm new to this and not sure what it means or how to fix it.

Upvotes: 0

Views: 799

Answers (2)

Md Abdul Gafur
Md Abdul Gafur

Reputation: 6201

Activity class is a thread. if you try to create thread inside activity without Handler it gives ThreadException. So, add a Handler to Handle this new thread.

Thanks

Upvotes: 0

Andro Selva
Andro Selva

Reputation: 54322

You are tying to Change a UI form a non UI thread which is not possible in Android. Instaed od calling this method setBackgroundDrawable(d); inside the run method of your Timer, surround it within a runonUiThread().

contextObj.runOnUiThread(new Runnable() {

        public void run() {
            // TODO Auto-generated method stub
            setBackgroundDrawable(d);
        }
    });

Try to get the context of your ACtivity and then change your LoadImage() like this,

private void loadimage(){
    try {
        url = new URL("http://3.bp.blogspot.com/_9UYLMDqrnnE/S4UgSrTt8LI/AAAAAAAADxI/drlWsmQ8HW0/s400/sachin_tendulkar_double_century.jpg");
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    InputStream content = null;
    try {
        content = (InputStream)url.getContent();
    } catch (IOException e) {
        e.printStackTrace();
    }
    final Drawable d = Drawable.createFromStream(content , "src"); 
    contextObj.runOnUiThread(new Runnable() {

        public void run() {
            // TODO Auto-generated method stub
            setBackgroundDrawable(d);
        }
    });


    setOnClickListener(this);
}

Upvotes: 1

Related Questions