I use childFragmentManager then
java.lang.Class
java.lang.Object.getClass()
has this error
It's my first fragment code and I have four child Fragment.
public class Main2Fragment extends Fragment {
Main2Season main2Season;
Main2Festival main2Festival;
Main2Local main2Local;
Main2Parking main2Parking;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_main2, container, false);
view.findViewById(R.id.season).setOnClickListener(mListener);
view.findViewById(R.id.festival).setOnClickListener(mListener);
view.findViewById(R.id.local).setOnClickListener(mListener);
view.findViewById(R.id.parking).setOnClickListener(mListener);
return view;
}
private final View.OnClickListener mListener = new View.OnClickListener() {
public void onClick(View view) {
switch (view.getId()) {
case R.id.season:
getChildFragmentManager().beginTransaction().replace(R.id.frag_container, main2Season).commit();
break;
case R.id.festival:
getChildFragmentManager().beginTransaction().replace(R.id.frag_container, main2Festival).commit();
break;
case R.id.local:
getChildFragmentManager().beginTransaction().replace(R.id.frag_container, main2Local).commit();
break;
case R.id.parking:
getChildFragmentManager().beginTransaction().replace(R.id.frag_container, main2Parking).commit();
break;
}
}
};
And error message is:
Process: com.example.bermuda, PID: 14194
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
CodePudding user response:
The problem is, you didn't instantiate your child fragments. Change your code to this:
private final View.OnClickListener mListener = new View.OnClickListener() {
public void onClick(View view) {
switch (view.getId()) {
case R.id.season:
getChildFragmentManager().beginTransaction().replace(R.id.frag_container, Main2Season()).commit();
break;
case R.id.festival:
getChildFragmentManager().beginTransaction().replace(R.id.frag_container, Main2Festival()).commit();
break;
case R.id.local:
getChildFragmentManager().beginTransaction().replace(R.id.frag_container, Main2Local()).commit();
break;
case R.id.parking:
getChildFragmentManager().beginTransaction().replace(R.id.frag_container, Main2Parking()).commit();
break;
}
}
};
CodePudding user response:
The problem here is clear, first you need to know what's a NullPointerException
.
Is normal, because you are declaring this :
Main2Season main2Season;
Main2Festival main2Festival;
Main2Local main2Local;
Main2Parking main2Parking;
And then without initialise them you use :
getChildFragmentManager().beginTransaction().replace(R.id.frag_container, main2Season).commit();
So doesn't matter which button you press, they'll fail.
So you need to initialise them before using them.
For instance you could do :
getChildFragmentManager().beginTransaction().replace(R.id.frag_container, Main2Season()).commit();