Picasso Load Image Failed and App Crashing

2019-02-20 13:29发布

问题:

I'm new to Android development, I'm trying to load image from url using Picasso, but it failed when I navigate to the Picasso loading activity.

Below is the code that i use for:

//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);

In my XML:

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

And my 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

I don't know what reason cause it failed. I've downloaded the picasso library from "http://square.github.io/picasso/", and added inside my library folder. Also, added INTERNET permission into my manifest file.

Any idea guys??

回答1:

From what I see, you spent a reference to the instance of the activity Context context = this; outside Oncreat method (), so be getting NullPointerException

Here

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

the context variable is null

Change this:

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

By:

//In onCreate()  

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


回答2:

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);

Note:cropImageView is ImageView



标签: picasso