毕加索:java.lang.IllegalArgumentException:Target不能为null

时间:2017-03-26 19:37:28

标签: java android picasso

Hey Guys尝试使用毕加索将图像放入图像视图时遇到了一些麻烦。

获取错误:java.lang.IllegalArgumentException:此代码行上的Target不能为null

            .into(UserProfilePhoto);

这是完整的代码 公共类UserProfileActivity扩展AppCompatActivity {     private final AppCompatActivity activity = UserProfileActivity.this;

private int avatarSize;
private String profilePhoto;


@BindView(R.id.UserProfilePhoto)
ImageView UserProfilePhoto;

@BindView(R.id.UserProfileTabs)
TabLayout UserProfileTabs;



protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ButterKnife.Bind(this);
    setContentView(R.layout.activity_user_profile);

    this.avatarSize = getResources().getDimensionPixelSize(R.dimen.user_profile_photo);
    this.profilePhoto = getString(R.string.user_profile_photo);

    Picasso.with(this)
            .load(profilePhoto)
            .placeholder(R.drawable.circle)
            .resize(avatarSize, avatarSize)
            .centerCrop()
            .transform(new TransformCircle())
            .into(UserProfilePhoto);

xml上的imageview与此处定义的相同,所以我不知道为什么它不起作用。

 java.lang.RuntimeException: Unable to start activity ComponentInfo{ie.coffeepal.coffeepal/ie.coffeepal.coffeepal.activities.UserProfileActivity}: java.lang.IllegalArgumentException: Target must not be null.
                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                                      at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                                      at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                      at android.os.Looper.loop(Looper.java:154)
                                                                      at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                   Caused by: java.lang.IllegalArgumentException: Target must not be null.
                                                                      at com.squareup.picasso.RequestCreator.into(RequestCreator.java:618)
                                                                      at com.squareup.picasso.RequestCreator.into(RequestCreator.java:601)
                                                                      at ie.coffeepal.coffeepal.activities.UserProfileActivity.onCreate(UserProfileActivity.java:49)
                                                                      at android.app.Activity.performCreate(Activity.java:6679)
                                                                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                                                                      at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                                                                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                      at android.os.Looper.loop(Looper.java:154) 
                                                                      at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                                      at java.lang.reflect.Method.invoke(Native Method) 
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

2 个答案:

答案 0 :(得分:3)

您正在使用ButterKnife,但从不调用ButterKnife.bind(this)方法,因此当您尝试加载图像时,ImageView为空。

UPD 在setContentView()方法

之后调用ButterKnife.bind(this)

答案 1 :(得分:2)

来自ButterKnife

Docmentation

您忘了拨打ButterKnife.bind(this);

所以您的imageview为空,这会在picasso

中产生异常

使用

 @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_profile);
    ButterKnife.bind(this);   //add this
    // TODO Use fields...
  }