单击单选按钮时,我的应用程序崩溃了。 无线电按钮是动态生成的。但是,当我提到那个按钮时,它会崩溃。
崩溃发生在这里显示小吃店。我相信什么时候 当我为所选Button调用方法getText时。
我该如何解决?
这是我的代码:
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);
}
}
}
这是我的Logcat
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)
无论如何,我不会&#39;知道它崩溃的原因。请帮忙
答案 0 :(得分:0)
在这里,我不清楚你的要求,但是没有想到在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);
}
}
答案 1 :(得分:0)
如果未选择单选按钮
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();
}
答案 2 :(得分:0)
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);
}
}
}