【问题标题】:Why am I getting "cannot resolve symbol urlDrawable" in this class为什么我在这个类中得到“无法解析符号 urlDrawable”
【发布时间】:2016-04-17 16:14:14
【问题描述】:

我正在尝试在 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);
            }
        }
    }
}

【问题讨论】:

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


    【解决方案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;
    

    【讨论】:

    • 我应该使用哪种方法?
    • 就在这一行的正上方:public UILImageGetter(View textView, Context context) {
    • 在“public class UILImageGetter implements Html.ImageGetter{”之后的类顶部,您有三个变量。底部的。
    • 更新了我的答案以显示包含错误的代码和您应该使用的代码
    • 谢谢,解决了。
    【解决方案2】:

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

    由于您没有为 urlDrawable 指定类型,编译器无法理解您正在声明一个变量。

    简单替换

    urlDrawable;
    

    UrlImageDownloader urlDrawable;
    

    【讨论】:

    • 我应该对urlDrawable 的所有实例都这样做还是只对第一个实例这样做?
    • 您需要为您声明的每个URLImageDownloader 类型的变量执行此操作。例如,我看到您已经为UrlImageDownloader mUrlImageDownloader; 这样做了。当然你只需要在declare a variable时这样做,而不是在你使用它的时候!
    猜你喜欢
    • 2021-05-02
    • 2021-08-27
    • 1970-01-01
    • 2015-02-28
    • 1970-01-01
    • 2017-04-16
    • 1970-01-01
    • 2014-08-19
    • 1970-01-01
    相关资源
    最近更新 更多