img*_*gen 5 android adt android-studio
我有一个使用Android Studio和ADT构建的应用程序.ADT是让其他人能够建立和运行的.我自己使用Android Studio.但最近当其他人添加了自定义UI控件时,Android Studio的版本将在具有自定义控件的UI中崩溃.经过深入调试后,我发现在Android Studio中,自定义控件的TypedArray具有与ADT版本不同的值.这是怎么发生的?我做了无数谷歌搜索没有成功.有人可以帮忙吗?
具体来说,getDimension(int index, float defValue)
就是抛出异常,它的代码如下:
public float getDimension(int index, float defValue) {
index *= AssetManager.STYLE_NUM_ENTRIES;
final int[] data = mData;
final int type = data[index+AssetManager.STYLE_TYPE];
if (type == TypedValue.TYPE_NULL) {
return defValue;
} else if (type == TypedValue.TYPE_DIMENSION) {
return TypedValue.complexToDimension(
data[index+AssetManager.STYLE_DATA], mResources.mMetrics);
}
throw new UnsupportedOperationException("Can't convert to dimension: type=0x"
+ Integer.toHexString(type));
}
Run Code Online (Sandbox Code Playgroud)
您可以看到索引的类型不是TYPE_DIMENSION.这里的问题是mData
,TypedArray
类的成员,在运行时具有与ADT编译的APK文件不同的值.这很奇怪.问题是Custom UI控件采用jar文件的形式,而不是源代码形式.我猜这是Android Studio用生成的DEX代码做的事情导致了这个问题.
更新:从Android Studio 0.5.8升级到Android Studio 0.5.9后,异常的位置稍有变化.现在getFloat
就是抛出异常.以下是源代码getFloat
/**
* Retrieve the float value for the attribute at <var>index</var>.
*
* @param index Index of attribute to retrieve.
*
* @return Attribute float value, or defValue if not defined..
*/
public float getFloat(int index, float defValue) {
index *= AssetManager.STYLE_NUM_ENTRIES;
final int[] data = mData;
final int type = data[index+AssetManager.STYLE_TYPE];
if (type == TypedValue.TYPE_NULL) {
return defValue;
} else if (type == TypedValue.TYPE_FLOAT) {
return Float.intBitsToFloat(data[index+AssetManager.STYLE_DATA]);
} else if (type >= TypedValue.TYPE_FIRST_INT
&& type <= TypedValue.TYPE_LAST_INT) {
return data[index+AssetManager.STYLE_DATA];
}
TypedValue v = mValue;
if (getValueAt(index, v)) {
Log.w(Resources.TAG, "Converting to float: " + v);
CharSequence str = v.coerceToString();
if (str != null) {
return Float.parseFloat(str.toString());
}
}
Log.w(Resources.TAG, "getFloat of bad type: 0x"
+ Integer.toHexString(type));
return defValue;
}
Run Code Online (Sandbox Code Playgroud)
错误发生在该行:
CharSequence str = v.coerceToString();
if (str != null) {
return Float.parseFloat(str.toString());
}
Run Code Online (Sandbox Code Playgroud)
由于从样式属性文件中检索到的错误type
导致的值不正确mData
,因此str变为
@2131296258
Run Code Online (Sandbox Code Playgroud)
而不是正确的价值
2131296258
Run Code Online (Sandbox Code Playgroud)
所以parseFloat
抱怨无效浮动值.
如您所见,根本原因是Android Studio中样式属性的值不正确.有关如何解决它的任何想法?
***顺便说一句,ADT版本完美无瑕.每当自定义UI小部件出现时,Android Studio版本才会崩溃
更新2: 以评论的形式部分回答了这个问题.但它没有得到解决.如果其他人需要信息,我会留下这样的话.
归档时间: |
|
查看次数: |
330 次 |
最近记录: |