Reputation: 3470
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);
}
}
}
Upvotes: 0
Views: 3372
Reputation: 869
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);
}
}
}
Upvotes: 1
Reputation: 23881
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.
Upvotes: 0
Reputation: 43
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
Upvotes: 1