java - 我想使用保存的首选项获取选定的单选按钮,但 Activity 没有打开?

标签 java android radio-button

这是代码

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)

最佳答案

似乎 mSex.getCheckedRadioButtonId() 没有返回正确的 ID。

因为这个 findViewById(selectedRadioButton) 无法找到 View 。因此,setSelected 方法调用中出现 NullPointerException。

关于java - 我想使用保存的首选项获取选定的单选按钮,但 Activity 没有打开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56672692/

相关文章:

java - 在没有单击按钮的情况下输入后,将 EditText 的值添加到 strings.xml 中的字符串

java - 如何更改所有屏幕上的背景图像并保留更改

android - 替代 Android 2.3.3 的 hasPermanentMenuKey()

ios - 如何在表格 View 中创建单选按钮

java - 在 Java 中调用该类的任何方法之前调用方法

java - JPA 2 构造函数表达式不能使用构造函数

java - JSOUP 提取 href 标题

android - 从 Android 通知中获取详细信息/扩展文本?

javascript - 如何使用 jquery 限制选中复选框的数量等于选择值?

GTK +如何找到选择了哪个单选按钮?