Solace
Solace

Reputation: 9010

AsyncTask- What is wrong with this type parameter?

This is the first program I was writing to practice using an AsyncTask. I failed to resolve this error, I tried to do a few suggested fixes, but that brought errors in the previous line. So I'll be thankful if somebody can suggest me what is wrong here and how to fix it.

package com.example.concurrency;

import java.io.InputStream;
import java.net.URL;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ImageView;

public class UsingAsyncTask extends Activity {
    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.using_asynctask); 
        String spec="http://www.google.com/imgres?imgurl=http%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2F7%2F7a%2FBasketball.png&imgrefurl=http%3A%2F%2Fcommons.wikimedia.org%2Fwiki%2FFile%3ABasketball.png&h=340&w=340&tbnid=EJmjEDyJzrhAuM%3A&zoom=1&docid=C_hn8nOgsGmuwM&hl=en&ei=Q0o2U93LNcaIygH4mICQBQ&tbm=isch&ved=0CHwQhBwwBg&iact=rc&dur=3875&page=1&start=0&ndsp=14";
        try {URL params= new URL(spec);} catch(Exception e) {}
        Bitmap result;
        new MyTask().execute(params);// ERROR:***** params cannot be resolved to a variable*************
        imageView= new ImageView(null);
    }

    private class MyTask extends AsyncTask<URL, Void, Bitmap> {

        private Bitmap loadImageFromNetwork(String url){
            try {
            Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(url).getContent());
            return bitmap;
            } catch (Exception e) {
            e.printStackTrace();
            }
            return null;
        }

        protected void onPreExecute() {

        }

        protected Bitmap doInBackground(String... params){
            //if(isCancelled()) return;
            String url=params.toString();
            final Bitmap bitmap= loadImageFromNetwork(url);
            return bitmap;
        }

        /*protected void onProgressUpdate(Progress progress){
            setProgressPercent(progress[0]);
        }*/

        protected void onPostExecute(Bitmap result){
            imageView.setImageBitmap(result);
        }

        @Override
        protected Bitmap doInBackground(URL... params) {
            // TODO Auto-generated method stub
            return null;
        }
    }

}

Upvotes: 0

Views: 783

Answers (3)

4mnes7y
4mnes7y

Reputation: 156

Be sure do declare params outside of the try-catch block to dodge the "params cannot be resolved to a variable" error.

You also have two doInBackground() methods in your AsyncTask. I imagine you would want to delete the second but change the type of the first to URL, or it will still fail.

package com.example.concurrency;

import java.io.InputStream;
import java.net.URL;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ImageView;

public class UsingAsyncTask extends Activity {
    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.using_asynctask); 
        String spec="http://www.google.com/imgres?imgurl=http%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2F7%2F7a%2FBasketball.png&imgrefurl=http%3A%2F%2Fcommons.wikimedia.org%2Fwiki%2FFile%3ABasketball.png&h=340&w=340&tbnid=EJmjEDyJzrhAuM%3A&zoom=1&docid=C_hn8nOgsGmuwM&hl=en&ei=Q0o2U93LNcaIygH4mICQBQ&tbm=isch&ved=0CHwQhBwwBg&iact=rc&dur=3875&page=1&start=0&ndsp=14";
        URL params;
        try {
            params = new URL(spec);
        } catch(Exception e) {}
        Bitmap result;
        new MyTask().execute(params);// ERROR:***** params cannot be resolved to a variable*************
        imageView= new ImageView(null);
    }
    private class MyTask extends AsyncTask<URL, Void, Bitmap> {

        private Bitmap loadImageFromNetwork(String url){
            try {
            Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(url).getContent());
            return bitmap;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        protected void onPreExecute() {

        }

        protected Bitmap doInBackground(URL... params){
            //if(isCancelled()) return;
            String url=params.toString();
            final Bitmap bitmap= loadImageFromNetwork(url);
            return bitmap;
        }

        /*protected void onProgressUpdate(Progress progress){
            setProgressPercent(progress[0]);
        }*/

        protected void onPostExecute(Bitmap result){
            imageView.setImageBitmap(result);
        }
    }
}

Upvotes: 1

markubik
markubik

Reputation: 671

try {URL params= new URL(spec);}

If you create variable params like this, it exists only in scope of try-catch block. So when you're executing asyncTask it, is not recognized. Do something like this:

   URL params;
   try {
     params = new URL(spec);
   }

Also as You wrote your code like above, asyncTask expects String as the first parameter (First parameter of asyncTask is what is what You receive as doInBackground parameter). Change doInBackground to:

protected Bitmap doInBackground(URL... params)

And then, get Your parameter by calling params[0] (because statement "URL..." means array of URLs with unknown size).

Upvotes: 5

codeMagic
codeMagic

Reputation: 44571

params shows not defined because you declare it inside of a try apart from where you try to use it. This puts it out of scope because if there is an exception then the variable won't be initialized.

Change it to something like

try {
    // here they can live peacefully together within the same scope
    URL params= new URL(spec);
    Bitmap result;
    new MyTask().execute(params);   
} catch(Exception e) {}

Also, you really should do something if you catch an exception. Leaving it blank like that won't do you any good and may very well lead to problems.

Upvotes: 3

Related Questions