lundi 19 juin 2017

get value of checkbox from a fragment to a class

i'm trying to get the value of a checkbox from a fragment "configFragment" to a class " BrowsingManager" . Since i can't do it through an intent, i m going with a method. But i get this error:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.widget.CheckBox.isChecked()' on a null object reference

This is my fragment :

/**
 * Created by xkbc1923 on 15/06/17.
 */

public class configFragment extends android.support.v4.app.Fragment {

    public CheckBox check;
public static Boolean checkBoxValue;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.config_layout, container, false);
    TextView titre=(TextView) rootView.findViewById(R.id.configu);
    check=(CheckBox) rootView.findViewById(R.id.checkbox_meat);
    TextView avertissement=(TextView) rootView.findViewById(R.id.avertissement);
    titre.setText("Configuration");
    avertissement.setText("Attention ! : Le code n'est pas chiffré!");


    BsSdkLog.d("checkbox"+checkBoxValue);

    return rootView;
}

public Boolean status(){
    if(check.isChecked())
    {
        checkBoxValue=true;
        return checkBoxValue;
    }else{
        checkBoxValue=false;
        return checkBoxValue;
    }
}
public Boolean getCheckBoxValue(){
   return checkBoxValue;
}

@Override
public void onPause() {
    super.onPause();
    save(check.isChecked());
}

@Override
public void onResume() {
    super.onResume();
    check.setChecked(load());
}

private void save(final boolean isChecked) {
    SharedPreferences sharedPreferences = getActivity().getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean("check", isChecked);
    editor.commit();
}

public boolean load() {
    SharedPreferences sharedPreferences = getActivity().getPreferences(Context.MODE_PRIVATE);
    return sharedPreferences.getBoolean("check", true);
}
}

and this is the class where i need the value of the checkbox :

public class BrowsingManager implements ClientAuthenticationApiListener {

public final static String FOLDERNAME_EXTERNAL_CALL = "_Coffre/";
   public final static String PREFRENCES_NAME = "Code";
private HomeViewModel mHomeViewModel;
public configFragment mconfig;

public void configureView(String orange_account_name) {
    String msisdn = null;
    String usergivenlogin = null;
    boolean isMobile = false;
    boolean failed = false;
    LowLevelAuthenticationIdentity aux = null;
    try {
        if (mClientAuthenticationApi != null) {
            LowLevelAuthenticationIdentity last = mClientAuthenticationApi.getLastStoredIdentity();
            LowLevelAuthenticationIdentity current = mClientAuthenticationApi.getCurrentSessionIdentity();
            aux = (orange_account_name == null) ? last : current;
            logIdentity("last stored identity", last);
            if (aux != null) {
                isMobile = aux.isMobileType();
                msisdn = aux.getMsisdn();
                usergivenlogin = aux.getUserGivenLogin();
            } else {
                failed = true;
            }
        } else {
            failed = true;
        }
    } catch (Exception e) {
        Crashlytics.logException(e);
        failed = true;
    }
    if (!failed) {
        BsApplication.sUserId = isMobile ? msisdn : usergivenlogin;
        Crashlytics.setUserIdentifier(BsApplication.sUserId);
        mHomeViewModel.setIdentifierMessage("Connecté en tant que : " + BsApplication.sUserId);
        mHomeViewModel.setPathVisibility(View.VISIBLE);
        mHomeViewModel.setConnectButtonVisibility(View.INVISIBLE);
        BsApplication.sWassupCookie = aux.getCookieValue();
        mHomeViewModel.setPath("");
        if (BuildConfig.PASSWORD_ASKED) {

            BsSdkLog.d("passwordasking-first-");
            //  testing(mHomeViewModel.getFragment().getActivity());
            BsSdkLog.d("Activity "+mHomeViewModel.getFragment().getActivity());
            Intent received= mHomeViewModel.getFragment().getActivity().getIntent();
            BsSdkLog.d("testing:" +received.getAction());
            if(received.getAction().equals(Intent.ACTION_MAIN)) {
                configFragment mconf=new configFragment();

                Boolean check1=mconf.status();;
                BsSdkLog.d("checked??"+check1);
                if(check1){
                        SharedPreferences settings = mHomeViewModel.getFragment().getActivity().getSharedPreferences(PREFRENCES_NAME, 0);
                        BsApplication.sPassword = settings.getString("pwd", "");
                        initBsAndCloudAndBrowse();
                }
                else{
                askForPassword();}
            }else {
                mHomeViewModel.setPath("");
                final Uri receivedUri = (Uri) received.getParcelableExtra(Intent.EXTRA_STREAM);
                //check we have a uri
                if (receivedUri != null) {
                    final boolean encrypt;
                    //set the picture
                    AlertDialog.Builder builder = new AlertDialog.Builder(mHomeViewModel.getFragment().getContext());
                    builder.setCancelable(true);
                    builder.setTitle("Choisir la méthode de stockage ");
                    //builder.setInverseBackgroundForced(true);
                    builder.setPositiveButton("Non chiffrée",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog,
                                                    int which) {

                                    askForPassword2(false);
                                    BsSdkLog.d("the onclick works");
                                    BsSdkLog.d(receivedUri.toString());


                                }
                            });
                    builder.setNegativeButton("Chiffrée",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog,
                                                    int which) {
                                    BsSdkLog.d("the onclick works");
                                    BsSdkLog.d(receivedUri.toString());
                                    askForPassword2(true);

                                }

                            });
                    AlertDialog alert = builder.create();
                    alert.show();
                }
            }
        } else {
            initBsAndCloudAndBrowse();
            //initBs();
        }

    } else {
        mHomeViewModel.setIdentifierMessage("");
        mHomeViewModel.setPathVisibility(View.INVISIBLE);
        mHomeViewModel.setRecyclerViewVisibility(View.INVISIBLE);
        mHomeViewModel.setConnectButtonVisibility(View.VISIBLE);
    }
}




Aucun commentaire:

Enregistrer un commentaire