这是代码
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) 原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.widget.RadioButton.setSelected(boolean)' 在com.teepe.teepe.KYC.personalData.onCreate(personalData.java:92) 在android.app.Activity.performCreate(Activity.java:6662)
答案 0 :(得分:1)
似乎mSex.getCheckedRadioButtonId()
没有返回正确的ID。
由于此findViewById(selectedRadioButton)
无法找到视图。因此,在setSelected
方法调用中为NullPointerException。
答案 1 :(得分:0)
在此找不到视图
RadioButton selectedSex = findViewById(selectedRadioButton);
必须像:
RadioButton selectedSex = findViewById(R.id.selectedRadioButton);