为什么我得到"无法解析符号urlDrawable"在这堂课上

时间:2016-04-17 16:14:15

标签: android android-studio android-volley android-json

我试图显示< img> json字符串中的标签和我使用this question中接受的答案,但Android Studio正在抱怨"无法解析符号urlDrawable

请有人告诉我为什么会这样,以及如何解决这个问题?

UILImageGetter

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.view.View;
import android.widget.TextView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;


public class UILImageGetter implements Html.ImageGetter{
    Context c;
    TextView conatiner;
    urlDrawable;

    public UILImageGetter(View textView, Context context) {
        this.c = context;
        this.conatiner = (TextView) textView;
    }

    @Override
    public Drawable getDrawable(String source) {
        urlDrawable = new UrlImageDownloader(c.getResources(), source);
        urlDrawable.drawable = c.getResources().getDrawable(R.drawable.default_thumb);

        ImageLoader.getInstance().loadImage(source, new SimpleListener(urlDrawable));
        return urlDrawable;
    }

    private class SimpleListener extends SimpleImageLoadingListener {
        UrlImageDownloader mUrlImageDownloader;

        public SimpleListener(UrlImageDownloader downloader) {
            super();
            mUrlImageDownloader= downloader;
        }

        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
            int width = loadedImage.getWidth();
            int height = loadedImage.getHeight();

            int newWidth = width;
            int newHeight = height;

            if (width > conatiner.getWidth()) {
                newWidth = conatiner.getWidth();
                newHeight = (newWidth * height) / width;
            }

            if (view != null) {
                view.getLayoutParams().width = newWidth;
                view.getLayoutParams().height = newHeight;
            }

            Drawable result = new BitmapDrawable(c.getResources(), loadedImage);
            result.setBounds(0, 0, newWidth, newHeight);

            mUrlImageDownloader.setBounds(0, 0, newWidth, newHeight);
            mUrlImageDownloader.mDrawable = result;

            conatiner.setHeight((conatiner.getHeight() + result.getIntrinsicHeight()));
            conatiner.invalidate();
        }

    }

    private class UrlImageDownloader extends BitmapDrawable {
        public  Drawable mDrawable;

        public UrlImageDownloader(Resources resources, String filepath) {
            super(resources, filepath);
            mDrawable = new BitmapDrawable(resources, filepath);
        }

        @Override
        public void draw(Canvas canvas) {
            if (mDrawable != null) {
                mDrawable.draw(canvas);
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您尚未为urlDrawable指定类型; 这部分代码包含错误:

public class UILImageGetter implements Html.ImageGetter{
    Context c;
    TextView conatiner;
    urlDrawable;

试试这个:

public class UILImageGetter implements Html.ImageGetter{
    Context c;
    TextView conatiner;
    UrlImageDownloader urlDrawable;

答案 1 :(得分:0)

Java是strongly typed language,这意味着必须将所有变量声明为某种类型。

由于您没有为urlDrawable指定类型,因此编译器并不了解您正在声明变量。

简单地替换

urlDrawable;

UrlImageDownloader urlDrawable;