0

I'm using picasso to load pics from web to my app's drawer menu. But now I got a problem which I've been trapped in for whole day here is my xml:

</FrameLayout>`<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">

    <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" />

</LinearLayout>

Here is my Java file:

public class MyMenuFragment extends MenuFragment {

private ImageView ivMenuUserProfilePhoto;

@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);
    setupHeader();
    return setupReveal(view) ;
}

private void setupHeader() {
    int avatarSize = getResources().getDimensionPixelSize(R.dimen.global_menu_avatar_size);
    String profilePhoto = getResources().getString(R.string.user_profile_photo);
    Picasso.with(getActivity())
            .load(profilePhoto)
            .placeholder(R.drawable.img_circle_placeholder)
            .resize(avatarSize, avatarSize)
            .centerCrop()
            .transform(new CircleTransformation())
            .into(ivMenuUserProfilePhoto);
}

Now when I run this project , the console reports the error" Caused by: java.lang.IllegalArgumentException: Target must not be null."

I debugged, found that the ivMenuUserProfilePhoto indeed is null, but I don't know why.how come that the findviewById doesn't work?

2 Answers2

0

It looks like you've accidentally moved to the start of the XML file rather than the end where it should be. You also added a ` which doesn't need to be there either.

So it should look like:

<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">

        <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" />

    </LinearLayout>
</FrameLayout>

Hope it fixes it :D

Patrick
  • 1,045
  • 1
  • 9
  • 21
0

getActivity() should not be used from onCreateView(), It may return null because fragment may not be completely attached with the activity. You should give a try calling setupHeader() from onAttach(Activity activity). You can refer this reference link - getActivity() returns null in Fragment function.

Community
  • 1
  • 1
Harsh4789
  • 677
  • 7
  • 17