安卓调试器隐藏局部变量
2022-09-03 09:57:33
在执行以下代码时,我使用Android的调试器时有一个奇怪的行为。变量值在被小部件初始化后立即消失。我把它移到手表上,但它说.无论我把变量放在哪里,在for循环之前还是在里面,它的行为无论如何都是一样的。我还打印了变量,正如您在代码中看到的那样,它说但是当我检查它时,它不会停止,最后在尝试将其转换为整数时会引发错误。"Cannot find local variable value"
"value is null"
if (value == null)
代码:
for (int i=0; i < (view != null ? ((ViewGroup)view).getChildCount() : 0); i++)
{
// Get name of the widget for example field__id,
// Convert to field name replacing field__id for id
// or for example field_name to name
// Check if the field exists in the column name, if so, add the ContentValue
View widget = ((ViewGroup)view).getChildAt(i);
String widgetName = view.getResources().getResourceEntryName(widget.getId());
String fieldName = widgetName.replace(Model.fieldPrefix,"");
Object value = null;
if (columnNames.contains(fieldName)) {
// TableField on the table matches the field on the form
try {
if (widget instanceof TextView) {
value = ((TextView) widget).getText().toString();
} else if (widget instanceof Spinner) {
value = ((SpinnerRow) ((Spinner) widget).getSelectedItem()).getId();
} else if (widget instanceof DatePicker) {
String date = AppDatabase.formatDateTime( getContext(), ((DatePicker) widget).getYear() + "-" + ((DatePicker) widget).getMonth() + "-" + ((DatePicker) widget).getDayOfMonth());
contentValues.put(fieldName, date ) ;
} else {
throw new ClassCastException("Could not cast the widget"+widget.getClass().toString());
}
Log.d(AppController.DEBUG_TAG, "Widget "+widgetName+" value is " + value.toString());
} catch (NullPointerException e) {
// Ignore exception:
value = null;
}
TableField tableField = this.getTable().getFieldByName(fieldName);
if ( (tableField.isPrimaryKey() && (value.equals("-1") || value.equals("")))
|| !tableField.getNotNull() && value.toString().length()==0 )
value = null;
if ( value == null || tableField.getType() == SQLiteCursor.FIELD_TYPE_NULL ) {
contentValues.putNull(fieldName);
} else if (tableField.getType() == SQLiteCursor.FIELD_TYPE_STRING || tableField.getType() == SQLiteCursor.FIELD_TYPE_VARCHAR) {
contentValues.put(fieldName, String.valueOf(value));
} else if (tableField.getType() == SQLiteCursor.FIELD_TYPE_INTEGER) {
contentValues.put(fieldName, Integer.valueOf(value.toString()) );
} else if (tableField.getType() == SQLiteCursor.FIELD_TYPE_FLOAT) {
contentValues.put(fieldName,Float.valueOf(value.toString()));
} else if (tableField.getType() == SQLiteCursor.FIELD_TYPE_BLOB) {
contentValues.put(fieldName,String.valueOf(value));
}
}
}