gpt4 book ai didi

java - 如何使用 HashTable 和 HashTable 中的键获取单选按钮 ID

转载 作者:行者123 更新时间:2023-12-01 11:59:15 25 4
gpt4 key购买 nike

我不太熟悉 HashTable 和使用 HashTable 动态制作 RadioButtons。我可以使用 HashTable 制作 RadioButtons,但无法获取 RadioButtons id 并且错误显示 java.lang.NullPointerException

这是我正在使用的代码

Map<String, List<String>> map = new HashMap<String, List<String>>();

final Hashtable<Integer, String> ht = new Hashtable<Integer, String>();
ht.put(1, "sachin");
ht.put(2, "sehwag");
ht.put(3, "dhoni");
final Enumeration<Integer> values = ht.keys();

while (values.hasMoreElements())
{ str = (Integer) values.nextElement();
System.out.println(str + ":" + ht.get(str));
RadioButton radioButtonView = new RadioButton(this);
radioButtonView.setText(ht.get(str));
radioGroup.addView(radioButtonView, p);
}


Button button =(Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
// checking Correct Answer
RadioButton selectedButton= (RadioButton)findViewById(radioGroup.getCheckedRadioButtonId());
String q_ans = selectedButton.getText().toString();
int selectedId = radioGroup.getCheckedRadioButtonId();
}
});

这是 CatLog 中显示的错误

  FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.testing.HashMaps$1.onClick(HashMaps.java:73)
at android.view.View.performClick(View.java:3517)
at android.view.View$PerformClick.run(View.java:14155)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:4624)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
at dalvik.system.NativeStart.main(Native Method)

请建议如何从 HashTable 获取 RadioButtons id 和 Key这样我就能走得更远提前致谢

最佳答案

您可以使用Key.set()从 HashMap 中获取Key以获取更多信息,请查看此链接Java Doc示例

Set<String> keys = h.keySet();
// Loop over String keys.
for (String key : keys) {
System.out.println(key);
}
您能告诉我您获取 RadioButtons id 的目的吗?之后我可以为您提供最适合您问题的准确解决方案谢谢

关于java - 如何使用 HashTable 和 HashTable 中的键获取单选按钮 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28109439/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com