0

I am trying to change the alpha on some images using a fragment in my main activity. However, everything I try I get the error:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference

I call the public function from my main activity like : Main Activity:

public void decadeDotsFragment(){

                DecadeDotsFragment fragment = new DecadeDotsFragment();
                FragmentManager manager = getSupportFragmentManager();
                manager.beginTransaction()
                        .replace(R.id.dots_fragment, fragment)
                        .addToBackStack(null)
                        .commit();

        fragment.selectDot(0);

    }

What am I doing wrong?

fragment:

public class DecadeDotsFragment extends Fragment {

    ImageView dot_0, dot_1, dot_2, dot_3;
    View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.decade_dots_indicator_fragment, container, false);

        return rootView;

    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState){

    }

    public void selectDot(int position){

        dot_0 = (ImageView) rootView.findViewById(R.id.dot_0);
        dot_1 = (ImageView) rootView.findViewById(R.id.dot_1);
        dot_2 = (ImageView) rootView.findViewById(R.id.dot_2);
        dot_3 = (ImageView) rootView.findViewById(R.id.dot_3);

        switch (position){
            case 0:
                dot_0.setAlpha((float) 1);
                dot_1.setAlpha((float) 0.5);
                dot_2.setAlpha((float) 0.5);
                dot_3.setAlpha((float) 0.5);
                break;
            case 1:
                dot_0.setAlpha((float) 0.5);
                dot_1.setAlpha((float) 1);
                dot_2.setAlpha((float) 0.5);
                dot_3.setAlpha((float) 0.5);
                break;
            case 2:
                dot_0.setAlpha((float) 0.5);
                dot_1.setAlpha((float) 0.5);
                dot_2.setAlpha((float) 1);
                dot_3.setAlpha((float) 0.5);
                break;
            case 3:
                dot_0.setAlpha((float) 0.5);
                dot_1.setAlpha((float) 0.5);
                dot_2.setAlpha((float) 0.5);
                dot_3.setAlpha((float) 1);
                break;
            default:
                dot_0.setAlpha((float) 0.5);
                dot_1.setAlpha((float) 0.5);
                dot_2.setAlpha((float) 0.5);
                dot_3.setAlpha((float) 0.5);
        }

    }


}

3 Answers 3

1

Do in proper way like this

public void decadeDotsFragment(){

    DecadeDotsFragment fragment = new DecadeDotsFragment();
    fragment.selectDot(0);//here should pass required position
    FragmentManager manager = getSupportFragmentManager();
    manager.beginTransaction()
            .replace(R.id.dots_fragment, fragment)
            .addToBackStack(null)
            .commit();
}

and call your method "selectDot()" from "onCreateView()" like this

public class DecadeDotsFragment extends Fragment {
    ImageView dot_0, dot_1, dot_2, dot_3;
    View rootView;
    private int mPosition;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.decade_dots_indicator_fragment, container, false);

        return rootView;

    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState){
       setData();
    }

    public void selectDot(int position){
        mPosition=position;
    }

    private void setData() {
        dot_0 = (ImageView) rootView.findViewById(R.id.dot_0);
        dot_1 = (ImageView) rootView.findViewById(R.id.dot_1);
        dot_2 = (ImageView) rootView.findViewById(R.id.dot_2);
        dot_3 = (ImageView) rootView.findViewById(R.id.dot_3);

        switch (mPosition){
            case 0:
                dot_0.setAlpha((float) 1);
                dot_1.setAlpha((float) 0.5);
                dot_2.setAlpha((float) 0.5);
                dot_3.setAlpha((float) 0.5);
                break;
            case 1:
                dot_0.setAlpha((float) 0.5);
                dot_1.setAlpha((float) 1);
                dot_2.setAlpha((float) 0.5);
                dot_3.setAlpha((float) 0.5);
                break;
            case 2:
                dot_0.setAlpha((float) 0.5);
                dot_1.setAlpha((float) 0.5);
                dot_2.setAlpha((float) 1);
                dot_3.setAlpha((float) 0.5);
                break;
            case 3:
                dot_0.setAlpha((float) 0.5);
                dot_1.setAlpha((float) 0.5);
                dot_2.setAlpha((float) 0.5);
                dot_3.setAlpha((float) 1);
                break;
            default:
                dot_0.setAlpha((float) 0.5);
                dot_1.setAlpha((float) 0.5);
                dot_2.setAlpha((float) 0.5);
                dot_3.setAlpha((float) 0.5);
        }

    }


}
9
  • Should be switch (mPosition) Commented Feb 26, 2017 at 17:09
  • Ok this is working first time.. How could I call it again once is created? In order to change the position again?
    – SNos
    Commented Feb 26, 2017 at 17:15
  • For example calling it from a button click event from main activity
    – SNos
    Commented Feb 26, 2017 at 17:16
  • You have to set Required position by calling this method "fragment.selectDot(should be your position here);" Commented Feb 26, 2017 at 17:17
  • Yes I have tried this but alpha won't change after created first time
    – SNos
    Commented Feb 26, 2017 at 17:18
1

This is the basic violation of Fragment . You are directly interacting with the fragment from the activity itself .

fragment.selectDot(0);

shouldn't be how you call that method. If you want to call any method in a fragment always create an interface and interact using it else it may cause strange issues like yours.

Tutorial :- Android Activity to Fragment Communication

1
  • Although enclosing fragment in an abstraction like this might be a good practice, that's not the root of the problem since even if cast to interface type, it's still a Fragment and there is no difference in managing the reference to it within the activity. Commented Feb 26, 2017 at 17:15
0

pass your position from Activity to fragment:

DecadeDotsFragment fragment = new DecadeDotsFragment();
    Bundle bundle = new Bundle();
    bundle.putInt("value", your_position);  //pass your position
    fragment.setArguments(bundle);    //send data
    FragmentManager manager = getSupportFragmentManager();
    manager.beginTransaction()
            .replace(R.id.dots_fragment, fragment)
            .addToBackStack(null)
            .commit();

Add the imageview reference in onCreateView()

Now in your Fragment use:

int position;   //Declare

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    rootView = inflater.inflate(R.layout.decade_dots_indicator_fragment, container, false);
     dot_0 = (ImageView) rootView.findViewById(R.id.dot_0);
    dot_1 = (ImageView) rootView.findViewById(R.id.dot_1);
    dot_2 = (ImageView) rootView.findViewById(R.id.dot_2);
    dot_3 = (ImageView) rootView.findViewById(R.id.dot_3);
    position = getArguments().getInt("value");  //get the position
    selectDot(position);   //call here
    return rootView;

}

Android framework expects your Fragment to create its view inside onCreateView() method. View becomes available after framework calls this method.

5
  • I have also tried this however, i get ` Attempt to invoke virtual method 'void android.widget.ImageView.setAlpha(float)' on a null object reference`
    – SNos
    Commented Feb 26, 2017 at 17:00
  • inside public void decadeDotsFragment() which is called in my main activity onCreate
    – SNos
    Commented Feb 26, 2017 at 17:04
  • move your selectDot() inside onViewCreated() Commented Feb 26, 2017 at 17:07
  • but if I move it there how can I call it agin if I need after called first time?
    – SNos
    Commented Feb 26, 2017 at 17:09
  • Thats working, but how can I call it again and change position once created for example using a button click event?
    – SNos
    Commented Feb 26, 2017 at 17:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.