I want to get the selected radiobutton using saved prefrences but the activity is not opening?

Chirag Gupta :

Here is the code

public RadioButton selectedSex;
public int selectedRadioButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    setContentView(R.layout.activity_personal_data);

    progressDialog = new ProgressDialog(this);

    // 0=FULL_NAME
    // 1 =BIRTHDAY
    //2=ADDRESS_LINE
    // 3=CITY
    //4=STATE
    // 5=PINCODE
    // 6=PHONE
    mUserPersonalInfo[0] = findViewById(R.id.user_name);
    mUserPersonalInfo[1]=findViewById(R.id.user_birth_date);
    mUserPersonalInfo[2]=findViewById(R.id.user_address_line);
    mUserPersonalInfo[3]=findViewById(R.id.user_city);
    mUserPersonalInfo[4]=findViewById(R.id.user_state);
    mUserPersonalInfo[5] = findViewById(R.id.userPhoneNumber);
    mUserPersonalInfo[6] = findViewById(R.id.user_pincode);
    mAuth = FirebaseAuth.getInstance();
    mSex=findViewById(R.id.user_sex);
    mNext = findViewById(R.id.personal_details_next_button);
    sharedpreferences = getSharedPreferences("personalPref", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedpreferences.edit();

    selectedRadioButton  = mSex.getCheckedRadioButtonId();
    selectedSex  = findViewById(selectedRadioButton);

    mUserPersonalInfo[1].setText( sharedpreferences.getString("userDOB", null));
    mUserPersonalInfo[0].setText(sharedpreferences.getString("userName", null));
    mUserPersonalInfo[2].setText(sharedpreferences.getString("userAddressline", null));
    mUserPersonalInfo[3].setText(sharedpreferences.getString("userCity", null));
    mUserPersonalInfo[4].setText( sharedpreferences.getString("userState", null));
    mUserPersonalInfo[5].setText( sharedpreferences.getString("userPhone", null));
    mUserPersonalInfo[6].setText(sharedpreferences.getString("userPincode", null));
    selectedSex.setSelected( sharedpreferences.getBoolean("userSex", false));
  //  mNext.setProgress(0);

   // FirebaseDatabase.getInstance().setPersistenceEnabled(true);
    mNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        //    mNext.setProgress(1);

            final String Birthday = mUserPersonalInfo[1].getText().toString();
            UserName = mUserPersonalInfo[0].getText().toString();
            final String Addressline = mUserPersonalInfo[2].getText().toString();
            final String City = mUserPersonalInfo[3].getText().toString();
            final String State = mUserPersonalInfo[4].getText().toString();
           PhoneNumber = mUserPersonalInfo[5].getText().toString();
            final String Pincode = mUserPersonalInfo[6].getText().toString();


   editor.putString("userName", UserName);
            editor.putString("userPhone", PhoneNumber);
            editor.putString("userDOB", Birthday);
            editor.putBoolean("userSex", selectedSex.isChecked());
            editor.putString("userAddressline", Addressline);
            editor.putString("userCity", City);
            editor.putString("userPincode", Pincode);
            editor.putString("userState", State);

            editor.apply();

Process: com.teepe.teepe, PID: 18864 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.teepe.teepe/com.teepe.teepe.KYC.personalData}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RadioButton.setSelected(boolean)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RadioButton.setSelected(boolean)' on a null object reference at com.teepe.teepe.KYC.personalData.onCreate(personalData.java:92) at android.app.Activity.performCreate(Activity.java:6662)

Deˣ :

It seems mSex.getCheckedRadioButtonId() is not returning correct Id.

Because of this findViewById(selectedRadioButton) is not able to find the View. and Hence, NullPointerException in the setSelected method call.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=156061&siteId=1