![](/img/trans.png)
[英]I have dynamically added radiogroups to an Activity. I can't get which Radiogroup is selected, but I can see the Radiobutton
[英]I want to get the selected radiobutton using saved prefrences but the activity is not opening?
這是代碼
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:嘗試調用虛擬在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 處 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) 處的空對象引用上的方法“void android.widget.RadioButton.setSelected(boolean)” 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) 由:java.lang.NullPointerException: Attempt to i 在 com.teepe.teepe.KYC.personalData.onCreate(personalData.java:92) at android.app.Activity.performCreate(Activity) 處的空對象引用上調用虛擬方法“void android.widget.RadioButton.setSelected(boolean)” .java:6662)
似乎mSex.getCheckedRadioButtonId()
沒有返回正確的 Id。
因此findViewById(selectedRadioButton)
無法找到視圖。 因此,在setSelected
方法調用中出現 NullPointerException。
在這找不到視圖
RadioButton selectedSex = findViewById(selectedRadioButton);
必須像:
RadioButton selectedSex = findViewById(R.id.selectedRadioButton);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.