AsyncTask-这个类型参数有什么问题?

时间:2014-04-02 19:03:49

标签: android android-asynctask

这是我写的第一个使用AsyncTask练习的程序。我没有解决这个错误,我试图做一些建议的修复,但这带来了前一行的错误。所以如果有人能告诉我这里有什么问题以及如何解决它,我会感激不尽。

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;
        }
    }

}

3 个答案:

答案 0 :(得分:5)

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

如果你创建这样的变量params,它只存在于try-catch块的范围内。因此,当您执行asyncTask时,无法识别。做这样的事情:

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

同样在编写上面的代码时,asyncTask期望String作为第一个参数(asyncTask的第一个参数是你收到的doInBackground参数)。将doInBackground更改为:

protected Bitmap doInBackground(URL... params)

然后,通过调用params [0]获取您的参数(因为语句“URL ...”表示大小未知的URL数组。)

答案 1 :(得分:3)

params显示未定义,因为除了尝试使用它之外,您在try内声明了它。这使它超出范围,因为如果存在异常,则不会初始化变量。

将其更改为

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) {}

另外,如果你遇到异常,你真的应该做某事。像这样留空对你没有任何好处,很可能会导致问题。

答案 2 :(得分:1)

确保在try-catch块之外声明params以避开&#34; params无法解析为变量&#34;错误。

您的AsyncTask中还有两个doInBackground()方法。我想你会想要删除第二个但是将第一个类型更改为URL,否则它仍然会失败。

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);
        }
    }
}