I have a weird behavior using Android's debugger when executing the following code. The variable value disappears just after it has been initialized by the widget. I moved it to to watches but it says "Cannot find local variable value"
. It does not matter where I place the variable, before the for loop or inside, it behaves the same no matter what. I also printed the variable as you can see in the code and it says "value is null"
but when I check it by if (value == null)
it does not stop and finally throws an error when trying to cast it to an integer.
The code:
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));
}
}
}
Do you use proguard with obfuscation?
If yes, that might be the problem - disable it with
-dontobfuscate
which you should put in your file with proguard rules (usually proguard-rules.txt, check your proguard config in the build.gradle file, like in the example below:
buildTypes {
debug {
runProguard true
zipAlign true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
signingConfig signingConfigs.debug
testCoverageEnabled true
}
}
I had the same problem. Finally, resolved by removing 'testCoverageEnabled true' from gradle build file.
A similar problem caused by a bug in the gradle version 1.0.0
which has beed solved in a later version 1.0.1
Search your project for 'com.android.tools.build:gradle:1.0.0'
which will be found in build.gradle
files. you may have more than one build.gradle
file if you are including modules.
whenever you have:
'com.android.tools.build:gradle:1.0.0'
Change it to:
'com.android.tools.build:gradle:1.0.1'
Then sync gradle. Android Studio will download the new gradle jar file and compiles with it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With