java.lang.IllegalArgumentException:Target不能为null。使用毕加索lib

时间:2016-08-04 08:26:38

标签: android picasso

我知道这个问题很多次,但我找不到我的解决方案。

我使用picasso在navigationDrawer中设置图像,但此错误会产生。

这是我的代码。

public class MyMenuFragment extends MenuFragment {

    private ImageView ivMenuUserProfilePhoto;
    private TextView ivMenuUserName;
    private NavigationView navigationView;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_menu, container,false);
        ivMenuUserProfilePhoto = (ImageView) view.findViewById(R.id.ivMenuUserProfilePhoto);
        ivMenuUserName = (TextView) view.findViewById(R.id.txtDpName);
        navigationView = (NavigationView) view.findViewById(R.id.vNavigation);
        setupHeader();
        return  setupReveal(view) ;
    }

    private void setupHeader() {
        Intent intent = getActivity().getIntent();

        int avatarSize = getResources().getDimensionPixelSize(R.dimen.global_menu_avatar_size);
        String profilePhoto = intent.getStringExtra("uri");
        Picasso.with(getActivity())
                .load(profilePhoto)
                .placeholder(R.drawable.img_circle_placeholder)
                .resize(avatarSize, avatarSize)
                .centerCrop()
                .transform(new CircleTransformation())
                .into(ivMenuUserProfilePhoto); <!-- error -->

        String profileName = intent.getStringExtra("dname");
        ivMenuUserName.setText(profileName);
    }
}

这是我的fragment_menu.xml

<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.NavigationView
            android:id="@+id/vNavigation"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@android:color/transparent"
            app:headerLayout="@layout/view_global_menu_header"
            app:itemIconTint="#8b8b8b"
            app:itemTextColor="#666666"
            app:menu="@menu/drawer_menu"/>
    </RelativeLayout>

view_global_menu_header.xml

  <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    android:clickable="true">

    <LinearLayout
        android:id="@+id/vGlobalMenuHeader"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp">

        <ImageView
            android:id="@+id/ivMenuUserProfilePhoto"
            android:layout_width="@dimen/global_menu_avatar_size"
            android:layout_height="@dimen/global_menu_avatar_size"
            android:layout_margin="12dp"
            android:src="@drawable/ic_menu_black" />

        <TextView
            android:id="@+id/txtDpName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:text="UserName"
            android:textColor="#2d5d82"
            android:textSize="16sp"
            android:textStyle="bold" />

    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_gravity="bottom"
        android:background="#dddddd" />
</FrameLayout>

我检查过每个ID都匹配..但它仍然是空的.PLZ帮助我解决这个问题。

2 个答案:

答案 0 :(得分:2)

改变这个:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_menu, container,false);
    ivMenuUserProfilePhoto = (ImageView) view.findViewById(R.id.ivMenuUserProfilePhoto);
    ivMenuUserName = (TextView) view.findViewById(R.id.txtDpName);
    navigationView = (NavigationView) view.findViewById(R.id.vNavigation);
    setupHeader();
    return  setupReveal(view) ;
}

对此:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_menu, container,false);
    ivMenuUserName = (TextView) view.findViewById(R.id.txtDpName);
    navigationView = (NavigationView) view.findViewById(R.id.vNavigation);
    ivMenuUserProfilePhoto = (ImageView) navigationView .findViewById(R.id.ivMenuUserProfilePhoto);
    setupHeader();
    return  setupReveal(view) ;
}

答案 1 :(得分:1)

 View navHeader = navigationView.getHeaderView(0);
 profilePictureView = (ImageView) navHeader.findViewById(R.id.personthumb);
 username = (TextView) navHeader.findViewById(R.id.userName);
 Glide.with(this).load(str).thumbnail(0.5f).into(profilePictureView);