如果我忽略其他无线电组,我可以得到文本,但我需要2列无线电组,因此我创建了另一个。从那时起,我无法从两个广播组中获取文本。这是代码:
//Continued on from above....
if (result.Header.Parameters.Response != ResponseCode.Ok)
{
//Something here!
}
这是logcat:
Save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
Connection con = connectionClass.CONN();
int selectedId = RadG.getCheckedRadioButtonId();
int selectedId1 = RadG1.getCheckedRadioButtonId();
RadioButton rb = (RadioButton) findViewById(selectedId);
RadioButton rb1 = (RadioButton) findViewById(selectedId1);
String Radbtn = rb.getText().toString().trim();
String Radbtn1 = rb1.getText().toString().trim();
String Vol = Vio.getText().toString();
getUID = UID.getText().toString();
String Contact = Cnumber.getText().toString();
if (rb1.isChecked()) {
String query = "Update Driver set ContactNumber='" + Contact + "', Violation='" + Radbtn + "',Comments='" + Vol + "' WHERE UID='" + getUID + "'";
PreparedStatement stmt = con.prepareStatement(query);
stmt.executeUpdate();
Toast.makeText(Main2Activity.this, "Saved", Toast.LENGTH_SHORT).show();
}
if (rb1.isChecked()) {
String query = "Update Driver set ContactNumber='" + Contact + "', Violation='" + Radbtn1 + "',Comments='" + Vol + "' WHERE UID='" + getUID + "'";
PreparedStatement stmt = con.prepareStatement(query);
stmt.executeUpdate();
Toast.makeText(Main2Activity.this, "Saved", Toast.LENGTH_SHORT).show();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
这是我的radiogroup的代码,我已经在使用OnChangelistener,我似乎无法找到为什么它仍然为我的getCheckedRadioButtonId返回null
04-12 22:19:43.904 25667-25667/com.orig3.thesis W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.RadioButton.getText()' on a null object reference
04-12 22:19:43.907 25667-25667/com.orig3.thesis W/System.err: at com.orig3.thesis.Main2Activity$5.onClick(Main2Activity.java:251)
04-12 22:19:43.907 25667-25667/com.orig3.thesis W/System.err: at android.view.View.performClick(View.java:6291)
04-12 22:19:43.907 25667-25667/com.orig3.thesis W/System.err: at android.view.View$PerformClick.run(View.java:24931)
04-12 22:19:43.908 25667-25667/com.orig3.thesis W/System.err: at android.os.Handler.handleCallback(Handler.java:808)
04-12 22:19:43.908 25667-25667/com.orig3.thesis W/System.err: at android.os.Handler.dispatchMessage(Handler.java:101)
04-12 22:19:43.908 25667-25667/com.orig3.thesis W/System.err: at android.os.Looper.loop(Looper.java:166)
04-12 22:19:43.908 25667-25667/com.orig3.thesis W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7390)
04-12 22:19:43.908 25667-25667/com.orig3.thesis W/System.err: at java.lang.reflect.Method.invoke(Native Method)
04-12 22:19:43.908 25667-25667/com.orig3.thesis W/System.err: at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
04-12 22:19:43.908 25667-25667/com.orig3.thesis W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:926)
04-12 22:19:46.349 25667-25674/com.orig3.thesis I/zygote64: Debugger is no longer active
答案 0 :(得分:0)
你在这两行之一上抛出了NullPointerException:
String Radbtn = rb.getText().toString().trim();
String Radbtn1 = rb1.getText().toString().trim();
这意味着您的某个单选按钮为空rb
或rb1
。它们由以下行初始化:
RadioButton rb = (RadioButton) findViewById(selectedId);
RadioButton rb1 = (RadioButton) findViewById(selectedId1);
一个为空意味着findViewById()
无法找到selectedId
作为ID的视图。您选择的ID之一不是有效值。你从这些方面获得它们:
int selectedId = RadG.getCheckedRadioButtonId();
int selectedId1 = RadG1.getCheckedRadioButtonId();
如果他们的nto有效则意味着getCheckRadioButtonId()返回错误。这意味着该组中没有选择radioButton。
(如果你调试了代码,你可以自己找到这个诊断)
<强>解决方案强> (编辑)的
您必须检查每个radioGroup中是否选择了某些内容,因为您可以在未选择任何内容时单击“保存”按钮。已解释为doc,如果未选择任何内容,getCheckedRadioButtonId
将返回-1。因此,添加if selectId!=-1
以确保选择了某些内容。