-1

hy everybody, i don't know why my code not save checkbox checked value; if i check checkbox and after i click on another tab and comeback to previous tab, checkbox is not selected...i hope that you find the error in my code!

IDE say me: FATAL EXCEPTION: main java.lang.NullPointerException at line ".onCreateView(holder.chkBox.setChecked(true);"

why?

thanks in advance!

ADAPTER CLASS:

public abstract class PlanetAdapter extends ArrayAdapter<Planet> implements CompoundButton.OnCheckedChangeListener

{

  private List<Planet> planetList;
  private Context context;
  ArrayList<Birra> objects;


  public  PlanetAdapter(List<Planet> planetList, Context context) {
      super(context, R.layout.single_listview_item, planetList);
      this.planetList = planetList;
      this.context = context;
  }




    public  class PlanetHolder  {
      public TextView planetName;
      public TextView distView;
      public TextView valuta;
      public CheckBox chkBox;
      public EditText edit;
      public String quantità;



  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {





      View row = convertView;
      PlanetHolder holder = null;
      if (row == null) {
          LayoutInflater inflater = ((Activity) context).getLayoutInflater();
          row = inflater.inflate(R.layout.single_listview_item, parent, false);
          holder = new PlanetHolder();
          holder.planetName = (TextView) row.findViewById(R.id.name);
          holder.distView = (TextView) row.findViewById(R.id.dist);
          holder.valuta = (TextView) row.findViewById(R.id.valuta);
          holder.chkBox = (CheckBox) row.findViewById(R.id.chk_box);
          holder.edit = (EditText) row.findViewById(R.id.editText);
          holder.edit.setVisibility(View.GONE);
          holder.edit.setEnabled(false);
          row.setTag(holder);
      } else {
          holder = (PlanetHolder) row.getTag();
      }
       final Planet p = planetList.get(position);


      final PlanetHolder finalHolder = holder;
      if(p.isCheckBoxchecked ())
      {
          finalHolder.chkBox.setChecked(true);
      }
      /*SharedPreferences preferences = getContext().getSharedPreferences("MaterialTabs", android.content.Context.MODE_PRIVATE);
      boolean mCheckBoxValue = preferences .getBoolean("CheckBox_Value", false);

      if (mCheckBoxValue) {

          holder.chkBox.setChecked(true);
      } else {
          holder.chkBox.setChecked(false);
      }*/


      holder.chkBox.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              if (finalHolder.chkBox.isChecked()) {
                  finalHolder.edit.setVisibility(View.VISIBLE);
                  finalHolder.edit.setEnabled(true);
                  SharedPreferences preferences = getContext().getSharedPreferences("MaterialTabs", android.content.Context.MODE_PRIVATE);
                 /* SharedPreferences.Editor editor = preferences.edit();
                  editor.putBoolean("showActivity", finalHolder.chkBox.isChecked());
                  editor.commit();*/
                 // boolean value= true;
                  SharedPreferences.Editor mEditor = preferences .edit();
                  mEditor.putBoolean("CheckBox_Value", finalHolder.chkBox.isChecked());

                  mEditor.commit();

                  finalHolder.edit.addTextChangedListener(new TextWatcher() {
                      @Override
                      public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                      }

                      @Override
                      public void onTextChanged(CharSequence s, int start, int before, int count) {
                      }

                      @Override
                      public void afterTextChanged(Editable s) {
                          p.setQuantità(finalHolder.edit.getText().toString().trim());
                          SharedPreferences preferences = getContext().getSharedPreferences("PROJECT_NAME", android.content.Context.MODE_PRIVATE);
                          SharedPreferences.Editor editor = preferences.edit();
                          editor.putString("KEY", finalHolder.edit.getText().toString().trim());

                          editor.commit();

                      }
                  });
              } else {
                  finalHolder.edit.setVisibility(View.GONE);
                  finalHolder.edit.setEnabled(false);
                  finalHolder.edit.setText(null);

              }

          }
      });
      holder.planetName.setText(p.getName());
      holder.distView.setText("" + p.getDistance());
      holder.valuta.setText(""+p.getValuta());
      holder.chkBox.setChecked(p.isSelected());
      holder.chkBox.setTag(p);
      holder.edit.setEnabled(false);

          return row;
  }


  ArrayList<Planet> getBox() {
      ArrayList<Planet> box = new ArrayList<Planet>();
      for (Planet p : planetList) {
          if (p.selected)
              box.add(p);
      }
      return box;
  }


}

FRAGMENT:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_list2, container, false);
    SharedPreferences preferences = getContext().getSharedPreferences("MaterialTabs", android.content.Context.MODE_PRIVATE);
      // preferences.getBoolean("showActivity", false);
       //preferences.getString("KEY", null);
    boolean mCheckBoxValue = preferences .getBoolean("CheckBox_Value", false);
    if (mCheckBoxValue) {
        holder.chkBox.setChecked(true);
    } else {
        holder.chkBox.setChecked(false);
    }





    Button mButton = (Button) rootView.findViewById(R.id.button);
    mButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
        showResult(v);


        }
    });
    //return inflater.inflate(R.layout.fragment_list2, container, false);
    return rootView;
}

PLANET:

class Planet {

    String name;
    int distance;
    String quantità;
    String valuta;
    boolean selected = false;
    boolean mCheckBoxState;


    public Planet(String name, int distance, String valuta) {
        super();
        this.name = name;
        this.distance = distance;
        this.valuta = valuta;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getDistance() {
        return distance;
    }

    public void setDistance(int distance) {
        this.distance = distance;
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }

    public String getQuantità() {
        return quantità;
    }

    public void setQuantità(String quantità) {
        this.quantità = quantità;
    }
    public String getValuta() {
        return valuta;
    }

    public void setValuta(String valuta) {
        this.valuta = valuta;
    }


    public boolean isCheckBoxchecked () { return mCheckBoxState; }
    private void setCheckBoxCheck(boolean state){ mCheckBoxState = state; }

}
Markella92
  • 71
  • 2
  • 13

2 Answers2

0

The fragment views will be refreshed each time so better option will be instead of storing it in preferences make use of the item (object) used to create the value For Ex in adapter carry out this operation final Planet p = planetList.get(position);

  final PlanetHolder finalHolder = holder;

  //set the check box if its previously checked
  if(p.isCheckBoxchecked)
  {
   finalHolder.chkBox.setChecked(true);
  }


  holder.chkBox.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
          if (finalHolder.chkBox.isChecked()) {
              finalHolder.edit.setVisibility(View.VISIBLE);
              finalHolder.edit.setEnabled(true);
              SharedPreferences preferences = getContext().getSharedPreferences("MaterialTabs", android.content.Context.MODE_PRIVATE);
             /* SharedPreferences.Editor editor = preferences.edit();
              editor.putBoolean("showActivity", finalHolder.chkBox.isChecked());
              editor.commit();*/
             // boolean value= true;
              SharedPreferences.Editor mEditor = preferences .edit();
              mEditor.putBoolean("CheckBox_Value", finalHolder.chkBox.isChecked());
              mEditor.commit();

              finalHolder.edit.addTextChangedListener(new TextWatcher() {
                  @Override
                  public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                  }

                  @Override
                  public void onTextChanged(CharSequence s, int start, int before, int count) {
                  }

                  @Override
                  public void afterTextChanged(Editable s) {

                  //set the item been checked
                  p.setCheckBoxCheck(true);


                  }
              });
          } else {
              finalHolder.edit.setVisibility(View.GONE);
              finalHolder.edit.setEnabled(false);
              finalHolder.edit.setText(null);

          }

      }
  });
witted_coder
  • 161
  • 1
  • 14
0

Try using this library to reduce complexity with your shared preferences related code. And if your shared preferences determine the state of views being displayed make sure you do relevant checks for stored values in the onCreate methods.

Viral Patel
  • 32,418
  • 18
  • 82
  • 110