0

In my aplication i have a fragment were i have any checkbox. When i chek start a TextToSpech. I have created a similar version without fragment and it work fine. But when i implemented it in a fragment it doesn't works. Her is my code:

public class OneFragment extends Fragment implements TextToSpeech.OnInitListener, CompoundButton.OnCheckedChangeListener {
private TextToSpeech tts;

ListView listView;
ArrayAdapter<Model> adapter;
List<Model> list = new ArrayList<Model>();

public OneFragment() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

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

    View returnView = inflater.inflate(R.layout.fragment_one, container, false);
    listView = (ListView) returnView.findViewById(R.id.listView);
    adapter = new MyAdapter(getActivity(), getModel());
    listView.setAdapter(adapter);
    final CheckBox chk = (CheckBox)returnView.findViewById(R.id.checkBox);
    chk.setOnCheckedChangeListener(OneFragment.this);

    return returnView;
}

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser) {
        tts = new TextToSpeech(getActivity(), this);
    }
}

private List<Model> getModel() {
    list.add(new Model("Linux", "S"));
    list.add(new Model("Linux", "S"));
    list.add(new Model("Linux", "S"));
    list.add(new Model("Linux", "S"));
    list.add(new Model("Linux", "S"));

    return list;
}

@Override
public void onInit(int status) {
    if (status == TextToSpeech.SUCCESS) {

        int result = tts.setLanguage(Locale.ITALIAN);

        if (result == TextToSpeech.LANG_MISSING_DATA
                || result == TextToSpeech.LANG_NOT_SUPPORTED) {
            Log.e("TTS", "This Language is not supported");
        } else {

            startSpeak();
        }

    } else {
        Log.e("TTS", "Initilization Failed!");
    }
}

private void startSpeak() {
    tts.speak("Ciao", TextToSpeech.QUEUE_FLUSH, null);
}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    CheckBox chk = (CheckBox)getActivity().findViewById(R.id.checkBox);
      for (Model i: list ) {
        if (chk.isChecked()) {
            this.startSpeak();
        }
    }
}
}

This is the partial LogCat result:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.CheckBox.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)' on a null object reference

How should i do?

  • I know what a null point exception is. But i don't understend because i have this problem when i use fragment. I have build a similar app that works. Now i would create the same app whit fragment (For swipe whit ViewPager) but i haved any problems, some i have fixed it other not. So why i have NullPointException when use fragment? – Marco ShepMonkey De Rossi Nov 23 '15 at 16:22

2 Answers2

3

i have a fragment were i have any checkbox

If CheckBox is inside Fragment layout in R.layout.fragment_one then use OneFragment.this.getView() to access CheckBox from layout instead of getActivity() :

CheckBox chk = (CheckBox)OneFragment.this.getView().findViewById(R.id.checkBox);

Or declare CheckBox chk as class level variable and initialize it inside onCreateView :

...
chk = (CheckBox)returnView.findViewById(R.id.checkBox);
...
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
0

try this in your onCheckedChanged method.

switch (buttonView.getId())
case R.id.checkBox:
/*
Do something
*/
break;