spa*_*ker 14 java debugging android undefined-behavior
在执行以下代码时,我使用Android的调试器有一种奇怪的行为.变量值在窗口小部件初始化后立即消失.我把它移到手表但它说"Cannot find local variable value".无论我在何处放置变量,在for循环之前或内部,无论如何都表现相同.我也打印了变量,你可以在代码中看到并且它说"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));
}
}
}
Run Code Online (Sandbox Code Playgroud)
Sas*_*lic 13
你是否使用了混淆的proguard?
如果是,那可能是问题 - 用它禁用它
-dontobfuscate
Run Code Online (Sandbox Code Playgroud)
你应该用proguard规则放入你的文件(通常是proguard-rules.txt,在build.gradle文件中检查你的proguard配置,如下例所示:
buildTypes {
debug {
runProguard true
zipAlign true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
signingConfig signingConfigs.debug
testCoverageEnabled true
}
}
Run Code Online (Sandbox Code Playgroud)
由gradle中的bug引起的类似问题version 1.0.0已经在以后解决了version 1.0.1
搜索'com.android.tools.build:gradle:1.0.0'可在build.gradle文件中找到的项目.build.gradle如果包含模块,则可能有多个文件.
无论何时你有:
'com.android.tools.build:gradle:1.0.0'
将其更改为:
'com.android.tools.build:gradle:1.0.1'
然后同步gradle.Android Studio将下载新的gradle jar文件并使用它进行编译.
| 归档时间: |
|
| 查看次数: |
12297 次 |
| 最近记录: |