我有一個導航活動由碎片組成,並且我有一個連接到onClickListener的按鈕,該按鈕運行onClick方法。該應用程序正常打開也是如此的片段,但每當我按一下按鈕,應用程序崩潰添加getCheckedRadioButtonId()時應用程序崩潰
public class FirstFragment extends Fragment implements View.OnClickListener {
View myView;
private RadioGroup radioGroup;
private Button btnDisplay;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View myView = inflater.inflate(R.layout.first_layout, container, false);
btnDisplay = (Button) myView.findViewById(R.id.btnDisplay);
btnDisplay.setOnClickListener(this);
return myView;
}
@Override
public void onClick(View v) {
radioGroup = (RadioGroup) v.findViewById(R.id.radio);
int selectedId = radioGroup.getCheckedRadioButtonId();
}
}
通過一些測試,我發現,這條線是造成問題。我需要這一行,因爲我想selectedId進入一個單選按鈕
int selectedId = radioGroup.getCheckedRadioButtonId();
沒有這一行,打開應用程序和片段正常,單擊該按鈕時,應用程序不會崩潰,我加了一個烤麪包和它沒有上面的代碼行工作正常。任何人都可以解釋如何解決此問題?謝謝
謝謝你,工作 – Taha
你的歡迎,快樂編碼 –