【发布时间】:2016-10-14 04:37:44
【问题描述】:
我正在使用
获取元素JSONArray myArray = new JSONArray();
for (View touchable : allTouchables) {
JSONObject j = new JSONObject();
// To check if touchable view is button or not
//if( touchable instanceof LinearLayout) {
//System.out.println("LinearLayout "+touchable.toString());
//}
if( touchable instanceof Button) {
System.out.println("Button "+touchable.getId());
}
if( touchable instanceof TextView) {
TextView question = (TextView) findViewById(touchable.getId());
System.out.println("TextView "+ question.getText().toString());
//j.put("key",value);
}
if( touchable instanceof RadioButton) {
RadioButton value = (RadioButton)findViewById(touchable.getId());
System.out.println("RadioButton "+value.getText().toString());
}
if( touchable instanceof CheckBox) {
CheckBox value = (CheckBox)findViewById(touchable.getId());
System.out.println("CheckBox "+value.getText().toString());
}
j.put("array",myArray);// error1.1
}
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“java.lang.CharSequence android.widget.TextView.getText()”
而error1.1是:
未处理的异常:org.json.JSONException
我的问题是:
- 如何使用
touchable.getId()获取textview单选按钮和复选框的id?因此,我可以获得有关该元素的更多信息。 - 修复异常。
【问题讨论】:
-
可以直接将touchable投给view,不需要findViewById,TextView question = (TextView)touchable;
-
@darwin 您可以在回答问题时添加答案,以便我们关闭 OP