2016-08-04 65 views
0

我知道这个问题很多次,但我找不到我的解决方案。java.lang.IllegalArgumentException:目标不能为空。而使用毕加索库存

我m使用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> 

我检查每个IDS是匹配..但它仍然得到空.. plz帮助我解决这个。

回答

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

现在产生的原因:显示java.lang.NullPointerException 在com.qwesys.ecommerce.fragment.MyMenuFragment.onCreateView(MyMenuFragment .java:34) –

+0

我的fragment_menu得到空指针异常 –

+0

不过谢谢,我的实际问题解决了 –