我正在使用
获取元素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?所以我可以获得有关所述元素的更多信息。答案 0 :(得分:1)
1)而不是findViewById(touchable.getId())
使用此
if( touchable instanceof TextView) {
TextView question = (TextView)touchable;
System.out.println("TextView "+ question.getText().toString());}
2)通过适当的try catch方法处理异常
try {
j.put("array",myArray);} catch (JSONException e) {e.printStackTrace();}