2014-09-02 66 views
1

我是Android開發新手,我試圖使用畢加索從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" 
    /> 

我的目錄下載:

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權限添加到我的清單文件中。

任何想法傢伙?

回答

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

它的工作原理!非常感謝! – willyip237 2014-09-02 01:55:52

+0

但我在全球範圍內使用上下文,甚至應用程序崩潰。 我在asyn任務中使用畢加索 – Prasad 2015-09-23 09:42:52

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的