毕加索加载图像失败和应用程序崩溃

时间:2014-09-02 01:20:23

标签: picasso

我是Android开发的新手,我正在尝试使用Picasso从url加载图像,但是当我导航到Picasso加载活动时它失败了。

以下是我用于的代码:

//Declaring Variable
ImageView ImageView1 = (ImageView)findViewById(R.id.forthImage);
Context context = this;

//In onCreate()
Picasso.with(context).load("http://postimg.org/image/wjidfl5pd/").into(ImageView1);

在我的XML中:

<ImageView
    android:id="@+id/forthImage"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/thirdImage"
    />

我的catLog:

09-02 09:11:39.632: E/AndroidRuntime(8680): FATAL EXCEPTION: main
09-02 09:11:39.632: E/AndroidRuntime(8680): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.toppan.toppan_android_v1/com.toppan.toppan_android_v1.Animation_Run}: java.lang.NullPointerException
09-02 09:11:39.632: E/AndroidRuntime(8680):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2151)
09-02 09:11:39.632: E/AndroidRuntime(8680):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286)
09-02 09:11:39.632: E/AndroidRuntime(8680):     at android.app.ActivityThread.access$600(ActivityThread.java:144)
09-02 09:11:39.632: E/AndroidRuntime(8680):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1259)
09-02 09:11:39.632: E/AndroidRuntime(8680):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-02 09:11:39.632: E/AndroidRuntime(8680):     at android.os.Looper.loop(Looper.java:137)
09-02 09:11:39.632: E/AndroidRuntime(8680):     at android.app.ActivityThread.main(ActivityThread.java:5166)
09-02 09:11:39.632: E/AndroidRuntime(8680):     at java.lang.reflect.Method.invokeNative(Native Method)
09-02 09:11:39.632: E/AndroidRuntime(8680):     at java.lang.reflect.Method.invoke(Method.java:525)
09-02 09:11:39.632: E/AndroidRuntime(8680):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:768)
09-02 09:11:39.632: E/AndroidRuntime(8680):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
09-02 09:11:39.632: E/AndroidRuntime(8680):     at dalvik.system.NativeStart.main(Native Method)
09-02 09:11:39.632: E/AndroidRuntime(8680): Caused by: java.lang.NullPointerException
09-02 09:11:39.632: E/AndroidRuntime(8680):     at android.app.Activity.findViewById(Activity.java:1902)
09-02 09:11:39.632: E/AndroidRuntime(8680):     at com.toppan.toppan_android_v1.Animation_Run.<init>(Animation_Run.java:17)
09-02 09:11:39.632: E/AndroidRuntime(8680):     at java.lang.Class.newInstanceImpl(Native Method)
09-02 09:11:39.632: E/AndroidRuntime(8680):     at java.lang.Class.newInstance(Class.java:1130)
09-02 09:11:39.632: E/AndroidRuntime(8680):     at android.app.Instrumentation.newActivity(Instrumentation.java:1064)
09-02 09:11:39.632: E/AndroidRuntime(8680):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2142)
09-02 09:11:39.632: E/AndroidRuntime(8680):     ... 11 more

我不知道是什么原因导致它失败了。我从“http://square.github.io/picasso/”下载了毕加索库,并添加到我的库文件夹中。 另外,在我的清单文件中添加了INTERNET权限。

任何想法的人?

2 个答案:

答案 0 :(得分:4)

final Target mTarget = new Target() {
        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom loadedFrom) {
            Log.d("DEBUG", "onBitmapLoaded");
            progress_bar.setVisibility(View.GONE);
            cropImageView.setImageBitmap(bitmap);
        }

        @Override
        public void onBitmapFailed(Drawable drawable) {
            Log.d("DEBUG", "onBitmapFailed");
        }

        @Override
        public void onPrepareLoad(Drawable drawable) {
            Log.d("DEBUG", "onPrepareLoad");
        }
    };
    Picasso.with(this).load(wallpaper.getMain_image()).into(mTarget);
    cropImageView.setTag(mTarget);

注意:cropImageView是ImageView

答案 1 :(得分:0)

从我看到的,你花了一个对活动实例的引用Context context = this; 在Oncreat方法()之外,所以得到NullPointerException

下面

Picasso.with(context).load("http://postimg.org/image/wjidfl5pd/").into(ImageView1);

context变量为空

改变这个:

//Declaring Variable
ImageView ImageView1 = (ImageView)findViewById(R.id.forthImage);
Context context = this;

人:

//In onCreate()  

ImageView ImageView1 = (ImageView)findViewById(R.id.forthImage);  
Picasso.with(this).load("http://postimg.org/image/wjidfl5pd/").into(ImageView1);