问题描述
我是Android开发的新手,我正在尝试使用Picasso从url加载图像,但是当我导航到Picasso加载活动时,它失败了.
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.
以下是我用于的代码:
//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:
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
我不知道是什么原因导致它失败.我已经从" http://square.github.io/picasso/"下载了毕加索库. ,并添加到我的媒体库文件夹中.另外,在我的清单文件中添加了INTERNET权限.
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.
有创意的人吗?
推荐答案
据我所知,您花了对活动Context context = this;
实例的引用在Oncreat方法()之外,因此要获取NullPointerException
From what I see, you spent a reference to the instance of the activity Context context = this;
outside Oncreat method (), so be getting 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);
这篇关于毕加索加载图像失败,应用崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!