2016-04-17 115 views
0

我想显示< IMG在JSON字符串>标签“无法解决符号urlDrawable”我使用公认的答案在this question而Android工作室抱怨“无法解析符号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); 
      } 
     } 
    } 
} 

回答

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; 
+0

在该方法中,我应该这样做吗? – Roseyk

+0

正好在这一行的上面:'public UILImageGetter(View textView,Context context){' – iamkaan

+0

'在公共类UILImageGetter实现Html.ImageGetter {'之后,你有三个变量。最底下的一个。 – ajohnston777

0

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

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

只需更换

urlDrawable; 

UrlImageDownloader urlDrawable; 
+0

我应该为'urlDrawable'的所有实例还是第一个? – Roseyk

+0

您需要为您声明的每个“URLImageDownloader”类型变量执行此操作。例如,我看到你已经在为'UrlImageDownloader mUrlImageDownloader;'做这件事。当然,当你[声明一个变量](http://java.about.com/od/understandingdatatypes/a/declaringvars.htm)时,你只需要这样做,而不是当你使用它时! – BadCash

相关问题