Я хочу получить выбранную радиокнопку, используя сохраненные настройки, но активность не открывается? - PullRequest
2 голосов
/ 19 июня 2019

Вот код

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();

Процесс: com.teepe.teepe, PID: 18864 java.lang.RuntimeException: Невозможно запустить действие ComponentInfo {com.teepe.teepe / com.teepe.teepe.KYC.personalData}: java.lang.NullPointerException: попытка вызвать виртуальный метод «void android.widget.RadioButton.setSelected (boolean)» для ссылки на пустой объект в android.app.ActivityThread.performLaunchActivity (ActivityThread.java): 2646) в android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2707) в android.app.ActivityThread.-wrap12 (ActivityThread.java) в android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1460) вandroid.os.Handler.dispatchMessage (Handler.java:102) на android.os.Looper.loop (Looper.java:154) на android.app.ActivityThread.main (ActivityThread.java:6077) на java.lang.reflect.Method.invoke (собственный метод) на com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:866) на com.android.internal.os.ZygoteInit.main (ZygoteInit.java:756). Причинаby: java.lang.NullPointerException: попытка вызвать виртуальный метод 'void android.widget.RadioButton.setSelected (boolean)' для пустой ссылки на объект в com.teepe.teepe.KYC.personalData.onCreate (personalData.java:92)atroid.app.Activity.performCreate (Activity.java:6662)

Ответы [ 2 ]

1 голос
/ 19 июня 2019

Кажется, mSex.getCheckedRadioButtonId() не возвращает правильный идентификатор.

Из-за этого findViewById(selectedRadioButton) не может найти представление.и, следовательно, NullPointerException в вызове метода setSelected.

0 голосов
/ 19 июня 2019

В этом нет поиска

RadioButton selectedSex = findViewById(selectedRadioButton);

должно быть как:

RadioButton selectedSex = findViewById(R.id.selectedRadioButton);

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...