M
M
max_vr2016-06-22 10:19:40
Java
max_vr, 2016-06-22 10:19:40

How to access image view in fragment?

I'm new, don't judge too harshly. I'm trying to do it this way:

public void accInfoFunc(){
        acc_frag = new infoAccount();
        fTrans = getFragmentManager().beginTransaction();
        fTrans.replace(R.id.fragmentCont, acc_frag);
        fTrans.commit();
        ImageView avaInInfo = (ImageView) findViewById(R.id.myAvaInInfo);
        avaInInfo.setImageResource(R.drawable.ic_menu_camera);
        //Picasso.with(this).load(avaUrl).into(avaInInfo);
    }

Crashes with an error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference

What is wrong in my code?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2016-06-22
@zagayevskiy

Everything is wrong in your code, starting with the naming.
No one outside the fragment should know what views it has inside (encapsulation). Pass all the data to the fragment through setArguments, get it inside using getArguments, set the picture in the onCreateView or onViewCreated method.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question