【发布时间】:2011-07-19 02:05:36
【问题描述】:
-- 更新问题--
我无法从我的自定义对话框中读取任何数据,起初我以为它只是单选按钮,所以我认为让我们继续项目,因此我为测试目的提供了一个默认值。
即使在我尝试从 EditText 中读取数据后,我还是会收到 nullpointerexceptions。
知道我在这里做错了什么吗? (下面是 XML + 代码,在下面的 cmets 中我展示了我对 EditText 所做的事情)
-- 老问题--
所以,我使用了多个来源来组合我现在使用的布局/检查功能。
当我试图查看选择了哪个单选按钮时,我不断收到空指针错误,所以我在这里搜索并添加了一些测试,看看我是否可以通过查看通过 radiogroup 设置的内容来获得结果(而不是单个按钮)。
让我向您展示我到目前为止所做/尝试过的事情。
public class MainActivity extends Activity {
private RadioGroup rbs; //added for test
private RadioButton rb1;
private RadioButton rb2;
private RadioButton rb3;
...
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.connectpopup);
dialog.setTitle("Quick Connect");
dialog.setCancelable(true);
Button button = (Button) dialog.findViewById(R.id.connect);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
rbs = (RadioGroup) findViewById(R.id.widget33); //added for test
rb1 = (RadioButton) findViewById(R.id.nmdc);
rb2 = (RadioButton) findViewById(R.id.adc);
rb3 = (RadioButton) findViewById(R.id.adcs);
//RadioGroup.getCheckedRadioButtonId()
Log.v("test", ""+rbs.getCheckedRadioButtonId()); //this doesn't give results either same nullpointer error
if (rb1.isChecked() == true) { // these check for checked makes the app stop.
Toast.makeText(getApplicationContext(), rb1.getText(), 5).show();
}
if (rb2.isChecked() == true) {
Toast.makeText(getApplicationContext(), rb2.getText(), 5).show();
}
if (rb3.isChecked() == true) {
Toast.makeText(getApplicationContext(), rb3.getText(), 5).show();
}
dialog.dismiss();
}
});
dialog.show();
以及我从中调用的 xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/quickconnectpopup"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView
android:id="@+id/help"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Please enter the address.\r\nSelect the protocol.\r\nEnter your nickname."
>
</TextView>
<EditText
android:id="@+id/hubaddress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="some.address.com:411"
android:textSize="18sp"
>
</EditText>
<RadioGroup
android:id="@+id/widget33"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RadioButton
android:id="@+id/nmdc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="protocol1"
>
</RadioButton>
<RadioButton
android:id="@+id/adc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="prototocol2"
>
</RadioButton>
<RadioButton
android:id="@+id/adcs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="protocol3"
>
</RadioButton>
</RadioGroup>
<EditText
android:id="@+id/nickname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Nick"
android:textSize="18sp"
>
</EditText>
<Button
android:id="@+id/connect"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Connect"
>
</Button>
</LinearLayout>
当我尝试读取(一两行)此行时出现错误(由于我稍后添加了 log.v,因此显示了同一行)
10:26:44.814 1124 ERROR AndroidRuntime Uncaught handler: thread main exiting due to uncaught exception
10:26:44.845 1124 ERROR AndroidRuntime java.lang.NullPointerException
10:26:44.845 1124 ERROR AndroidRuntime at nl.my.project.MainActivity$1.onClick(MainActivity.java:89)
Log.v("test", ""+rbs.getCheckedRadioButtonId()); //this doesn't give results either same nullpointer error
if (rb1.isChecked() == true) { // these check for checked makes the app stop.
所以再次陈述确切的问题,我怎样才能检索选定的单选按钮。
在示例中显示在 Log.v("selected button:", variable); =
Verbose Androidblabla radio1
【问题讨论】:
-
即使只是一个想法也可能奏效,我现在一无所知。如果您没有立即的解决方案,但认为您已经看到了一些东西,我可以再次开始使用谷歌搜索,用我描述中的 3 个词,我再也无法在谷歌上找到任何有用的结果。
-
YaHubAddress = (EditText) findViewById(R.id.hubaddress); Log.v("NMDC", YaHubAddress.getText().toString());这给出了完全相同的错误:S 似乎我根本无法从对话框中读取任何数据:S
-
答案仅用于此目的,答案。将来,只需编辑您的问题以提供说明。 Stack Overflow 不是论坛 :)