无法从Url获​​取图像

时间:2014-07-02 14:42:10

标签: android

使用https://github.com/nostra13/Android-Universal-Image-Loader

    ImageLoader imageLoader = ImageLoader.getInstance();
    imageLoader.init(ImageLoaderConfiguration.createDefault(context));
    imageLoader.displayImage(player.avatar, holder.avatar);

player.avatar是一个图片网址

holder.avatar是一个imageview

07-02 18:35:22.891  25096-25096/ravsii.my.dotabuff E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: ravsii.my.dotabuff, PID: 25096
java.lang.IllegalArgumentException: view must not be null
        at com.nostra13.universalimageloader.core.imageaware.ViewAware.<init>(ViewAware.java:70)
        at com.nostra13.universalimageloader.core.imageaware.ViewAware.<init>(ViewAware.java:50)
        at com.nostra13.universalimageloader.core.imageaware.ImageViewAware.<init>(ImageViewAware.java:43)
        at com.nostra13.universalimageloader.core.ImageLoader.displayImage(ImageLoader.java:286)
        at ravsii.my.dotabuff.SearchPlayerAdapter.getView(SearchPlayerAdapted.java:66)
        at android.widget.AbsListView.obtainView(AbsListView.java:2255)
        at android.widget.ListView.makeAndAddView(ListView.java:1790)
        at android.widget.ListView.fillDown(ListView.java:691)
        at android.widget.ListView.fillFromTop(ListView.java:752)
        at android.widget.ListView.layoutChildren(ListView.java:1630)
        at android.widget.AbsListView.onLayout(AbsListView.java:2087)
        at android.view.View.layout(View.java:14817)
        at android.view.ViewGroup.layout(ViewGroup.java:4631)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
        at android.view.View.layout(View.java:14817)
        at android.view.ViewGroup.layout(ViewGroup.java:4631)
        at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:374)
        at android.view.View.layout(View.java:14817)
        at android.view.ViewGroup.layout(ViewGroup.java:4631)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
        at android.view.View.layout(View.java:14817)
        at android.view.ViewGroup.layout(ViewGroup.java:4631)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1983)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1740)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
        at android.view.Choreographer.doCallbacks(Choreographer.java:574)
        at android.view.Choreographer.doFrame(Choreographer.java:544)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)

我还尝试使用替代方法,使用位图和可绘制资源,但它总是返回null。 抱歉我的英语:)

2 个答案:

答案 0 :(得分:0)

为什么不使用Android Developer的教程?

// show The Image
new DownloadImageTask((ImageView) findViewById(R.id.imageView1))
            .execute("http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png");
}

public void onClick(View v) {
    startActivity(new Intent(this, IndexActivity.class));
    finish();

}

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    ImageView bmImage;

    public DownloadImageTask(ImageView bmImage) {
        this.bmImage = bmImage;
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }

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

添加权限

<uses-permission android:name="android.permission.INTERNET" />

答案 1 :(得分:0)

我还建议调查毕加索图书馆。将图像从URL加载到图像视图中,这是一个天赐之物。

http://square.github.io/picasso/