teej2542
teej2542

Reputation: 608

Using picasso inside a NavigationView

I am trying to use picasso in an Activity and using a NavigationView(for a Drawer).

Here is the latest of what I have.

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_drawer);

        mDrawerLayout = findViewById(R.id.drawer_main_search_layout);
        final NavigationView navigationView = findViewById(R.id.nav_view_main_search);
        navigationView.setNavigationItemSelectedListener(this);
        userEmail = navigationView.getHeaderView(0).findViewById(R.id.nav_user_email);
        profilePic = findViewById(R.id.profile_pic_drawer);

        FirebaseAuth auth = FirebaseAuth.getInstance();
        if (auth.getCurrentUser() != null) {
            userEmail.setText(auth.getCurrentUser().getEmail());
            uuid = auth.getCurrentUser().getUid();
            Log.d(TAG, "email_test_log: " + auth.getCurrentUser().getEmail());

            storageRef.child(uuid + "/profile_pic/main.jpg").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                @Override
                public void onSuccess(Uri uri) {
                    // Got the download URL for 'users/me/profile.png'
                    Log.d(TAG, "TEST_PIC_LOC: "+ uri);
                    Picasso.with(navigationView.getHeaderView(0).getContext()).load(uri).into(profilePic);
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception exception) {
                    // Handle any errors
                }
            });
        }
        showFragment(new SearchFragment());
    }

The error is with this line

(Picasso.with(navigationView.getHeaderView(0).getContext()).load(uri).into(profilePic);)

The error is

java.lang.IllegalArgumentException: Target must not be null.

I have tried different variations such as "this", but doesn't compile, "getContext", "getApplication" but the app crashes.

Upvotes: 0

Views: 140

Answers (3)

SonVi
SonVi

Reputation: 91

change:

 profilePic = findViewById(R.id.profile_pic_drawer);
 Picasso.with(navigationView.getHeaderView(0).getContext()).load(uri).into(profilePic);

by:

profilePic = (ImageView)findViewById(R.id.profile_pic_drawer);
Picasso.with(getApplicationContext()).load(uri).into(profilePic);

Or check your uri value, may be it null

Upvotes: 2

azamatikus
azamatikus

Reputation: 73

Try this:

Picasso.with(getAplicationContext()).load(uri).into(profilePic);

Upvotes: 1

Uthaya
Uthaya

Reputation: 373

There two possibility for this error one is Resource id is null or Your context may be null.

CheckYour Resource ID:

R.id.profile_pic_drawer // check this available or not

Second is Your activity context pass like this :

Picasso.with(YourActivityName.this).load(uri).into(profilePic);

Still getting error just show me the code i will help you.

Upvotes: 1

Related Questions