当我单击单选按钮时,我的应用程序崩溃了。单选按钮是动态生成的。但是,当我提到该按钮时,它会崩溃。
崩溃发生在这里,当显示小吃栏时。我相信当 当我为所选按钮调用方法获取文本时。
我该如何解决?
这是我的代码:
public class ListActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
String[] filename = getApplicationContext().fileList();
addRadioButtons(filename.length, filename);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
RadioGroup rd = (RadioGroup) findViewById(R.id.file_radio_group);
int id_Btn = rd.getCheckedRadioButtonId() ;
RadioButton selectedRB = (RadioButton) findViewById(id_Btn) ;
Snackbar.make(view, "Replace with your own action "+selectedRB.getText().toString(), Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
public void addRadioButtons(int number,String[] filenames) {
for (int row = 0; row < 1; row++) {
RadioGroup ll = new RadioGroup(this);
ll.setOrientation(LinearLayout.VERTICAL);
for (int i = 1; i <= number; i++) {
RadioButton rdbtn = new RadioButton(this);
rdbtn.setId((row * 2) + i);
rdbtn.setText(filenames[i-1]);
//rdbtn.setText("Radio " + rdbtn.getId());
rdbtn.setTextSize(25);
ll.addView(rdbtn);
}
((ViewGroup) findViewById(R.id.file_radio_group)).addView(ll);
}
}
}
这是我的日志猫
11-01 19:04:26.460 6743-6743/alahdal.amjad.newloginproject E/AndroidRuntime: FATAL EXCEPTION: main
Process: alahdal.amjad.newloginproject, PID: 6743
java.lang.NullPointerExceptionat
alahdal.amjad.newloginproject.ListActivity$1.onClick(ListActivity.java:39)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515) atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
无论如何,我不知道它为什么崩溃。请帮忙
你
去吧,我不清楚你的要求,但不要想到在 xml 中已经有一个单选组中添加另一个单选组。这是更正的版本工作正常。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final RadioGroup radioGroup = (RadioGroup) findViewById(R.id.file_radio_group);
String[] filename = getApplicationContext().fileList();
addRadioButtons(radioGroup, filename.length, filename);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int id_Btn = radioGroup.getCheckedRadioButtonId();
for (int count = 0; count < radioGroup.getChildCount(); count++) {
if (radioGroup.getChildAt(count).getId() == id_Btn) {
RadioButton radioButton = (RadioButton) radioGroup.getChildAt(count);
Snackbar.make(view, "Replace with your own action " + radioButton.getText().toString(), Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
}
}
});
}
public void addRadioButtons(RadioGroup radioGroup, int number, String[] filenames) {
radioGroup.setOrientation(LinearLayout.VERTICAL);
for (int i = 1; i <= number; i++) {
RadioButton rdbtn = new RadioButton(this);
rdbtn.setId((i * 2) + i);
rdbtn.setText(filenames[i - 1]);
rdbtn.setTextSize(25);
radioGroup.addView(rdbtn);
}
}
如果未选择单选按钮 rd.getCheckedRadioButtonId()
将返回 -1,这不是任何视图的 ID,因此findViewById
找不到视图。
在使用 id_Btn
查找视图之前检查 -1。
public void onClick(View view) {
RadioGroup rd = (RadioGroup) findViewById(R.id.file_radio_group);
int id_Btn = rd.getCheckedRadioButtonId() ;
if (id_Btn != -1) {
RadioButton selectedRB = (RadioButton) findViewById(id_Btn) ;
} else {
//Handle case if no button is selected
}
Snackbar.make(view, "Replace with your own action "+selectedRB.getText().toString(), Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
public class ListActivity extends AppCompatActivity {
private RadioGroup ll;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
String[] filename = getApplicationContext().fileList();
addRadioButtons(filename.length, filename);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
RadioGroup rd = (RadioGroup) findViewById(R.id.file_radio_group);
int id_Btn = rd.getCheckedRadioButtonId() ;
RadioButton selectedRB = (RadioButton) findViewById(id_Btn) ;
Snackbar.make(view, "Replace with your own action "+selectedRB.getText().toString(), Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
public void addRadioButtons(int number,String[] filenames) {
for (int row = 0; row < 1; row++) {
ll = new RadioGroup(this);
ll.setOrientation(LinearLayout.VERTICAL);
for (int i = 1; i <= number; i++) {
RadioButton rdbtn = new RadioButton(this);
rdbtn.setId((row * 2) + i);
rdbtn.setText(filenames[i-1]);
//rdbtn.setText("Radio " + rdbtn.getId());
rdbtn.setTextSize(25);
ll.addView(rdbtn);
}
((ViewGroup) findViewById(R.id.file_radio_group)).addView(ll);
}
}
}