Witajcie,
próbuje dynamicznie podmieniać Fragmenty
, ale gdy odpalam aplikacje to się zaraz rozwala. Nie wyświetla się żaden błąd w kodzie ale jednak coś musi być nie tak. Do momentu kiedy nie zacząłem grzebać we fragmentList.add(new Fragment1(null, R.drawable.image_file, null, true));
wszystko grało.
MainActivity
fragmentList = new ArrayList<>();
fragmentList.add(new Fragment1(getResources().getString(R.string.text_page_1), R.drawable.swans, new String[]{getResources().getString(R.string.answer1), getResources().getString(R.string.answer2),getResources().getString(R.string.answer3)},false));
fragmentList.add(new Fragment1(null, R.drawable.image_file, null, true));
fragmentList.add(new Fragment1(getResources().getString(R.string.text_page_2), R.drawable.nature, new String[]{getResources().getString(R.string.answer4), getResources().getString(R.string.answer5),getResources().getString(R.string.answer6)},false));```
Fragment1
public class Fragment1 extends Fragment {
String stringValue;
int imagesResId;
TextView text;
String[] rbData;
RadioGroup radioButtons;
boolean mapImage;
ImageView maps;
View answer;
public Fragment1(String str, int imageView , String[] rb, boolean arg) {
this.stringValue = str;
this.imagesResId = imageView;
this.rbData = rb;
this.mapImage = arg;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.i("x","onCreateViewFragment");
View view = inflater.inflate(R.layout.fragment_1, container, false);
text = view.findViewById(R.id.textView);
ImageView imageResId = view.findViewById(image);
maps = view.findViewById(map_images);
answer = view.findViewById((R.id.radioGroup));
text.setText(stringValue);
imageResId.setImageResource(imagesResId);
maps.setImageResource(imagesResId);
if(mapImage){
view = inflater.inflate(R.layout.maps, container, false);
maps = view.findViewById(R.id.map_images);
maps.setImageResource(imagesResId);
}else{
view = inflater.inflate(R.layout.fragment_1, container, false);
text = view.findViewById(R.id.textView);
radioButtons = view.findViewById(R.id.radioGroup);
text.setText(stringValue);
imageResId.setImageResource(imagesResId);
//checkboxes, textviews, imageviews, etc
}
if (answer != null) {
for (int i = 0; i < radioButtons.getChildCount(); i++) {
((RadioButton) radioButtons.getChildAt(i)).setText(rbData[i]);
}
}
return view;
}
Uaktualnienie postu, log:
10-30 17:22:53.281 14867-14867/? E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at make.appaplication.Fragment1.onCreateView(Fragment1.java:64)
Chodzi o tą linię kodu w metodzie onCreateView
Fragment
maps.setImageResource(imagesResId);