Android UnsupportedOperationException: Can't convert to color: type=0x2

匿名 (未验证) 提交于 2019-12-03 01:47:02

问题:

This is the crashlog from Android Market:

android.view.InflateException: Binary XML file line #8: Error inflating class  at android.view.LayoutInflater.createView(LayoutInflater.java:596) at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) at android.view.LayoutInflater.onCreateView(LayoutInflater.java:644) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669) at android.view.LayoutInflater.rInflate(LayoutInflater.java:724) at android.view.LayoutInflater.inflate(LayoutInflater.java:479) at android.view.LayoutInflater.inflate(LayoutInflater.java:391) at android.view.LayoutInflater.inflate(LayoutInflater.java:347) at com.designfuture.music.ui.dialog.MXMDialog.(MXMDialog.java:73) at com.designfuture.music.ui.dialog.MXMDialog.(MXMDialog.java:58) at com.designfuture.music.model.MXMStoreBuy.getBuyDialog(MXMStoreBuy.java:70) at com.designfuture.music.ui.fragment.plbl.XLBLFragment$ViewHolder$3.onClick(XLBLFragment.java:290) at android.view.View.performClick(View.java:3110) at android.view.View$PerformClick.run(View.java:11934) at android.os.Handler.handleCallback(Handler.java:587) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:132) at android.app.ActivityThread.main(ActivityThread.java:4123) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:491) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Constructor.constructNative(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:416) at android.view.LayoutInflater.createView(LayoutInflater.java:576) ... 22 more Caused by: java.lang.UnsupportedOperationException: Can't convert to color: type=0x2 at android.content.res.TypedArray.getColor(TypedArray.java:326) at android.widget.TextView.(TextView.java:422) at android.widget.TextView.(TextView.java:364) ... 25 more 

So for what I can see the problem is in

at com.designfuture.music.ui.dialog.MXMDialog.(MXMDialog.java:73) 

Where I have this line of code:

mHolder = new ViewHolder((ViewGroup) inflater.inflate(R.layout.dialog_mxm, null)); 

This is the complete function

public MXMDialog(Context context, String title) {         super( context, R.style.Dialog_MXM );          //getWindow()         LayoutParams lp= getWindow().getAttributes();         lp.gravity = Gravity.CENTER;         lp.dimAmount = 0.5f;         lp.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL;          if(inflater == null)             inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);          mHolder = new ViewHolder((ViewGroup) inflater.inflate(R.layout.dialog_mxm, null));         super.setContentView(mHolder.root);          if(title != null)             setTitle(title);     } 

This is the content of the R.layout.dialog_mxm

I think that the problem is in the TextView. The style of that textview cannot be a problem because comes directly from Android so I don't think this will be a problem.

The android:textColor="@color/orange_mxm" is this in my colors.xml

#FFff6500

This is the Dialog.MXM style

      

And I don't think this is a so not-standard color...

Where's the problem? How can I solve this?

回答1:

Does the Dialog.MXM style vary with the API level supported by the device (e.g. using a values-v14/ directory) or is it used for all devices?

The DeviceDefault theme family (including Theme.DeviceDefault.Dialog) was added in API 14 (Android 4.0, ICS) and will not be present on older devices. This means that on those devices where that theme cannot be found, Dialog.MXM in effect has a null parent theme. Themes expect a number of attributes to have defined values, and in this case your theme will be missing many required ones. Some devices may deal with this more gracefully than others.

One solution is to define a common base theme that inherits from a different parent based on which platform version the device is running. An example:

res/values/themes.xml:

[...]   [...] 

res/values-v11/themes.xml:

[...]   [...] 

res/values-v14/themes.xml:

[...]   [...] 


回答2:

I was getting this error in one of my modules. Here's the issue description :

there's a color item for 'white' color defined in colors.xml of main app module.

#ffffff

And in one of the layouts in my module, I'm referring to the white color as :

The mistake I did was I have defined the item color 'white' again in my module's color.xml as

**@color/white** 

Instead It should have been like

#ffffff. 

That was my issue.

Thanks, Rakesh



回答3:

I got this exception, but for a different reason. I was trying to reference a custom attribute from a selector drawable. It is a known issue on pre-Lollipop devices as explained here and here.

In case anyone needs to do that (and keep backward compatibility), one workaround is to switch themes, define a separate drawable for each theme and create a custom attribute for the drawable in attrs.xml. An example is given in the accepted answer in the first link above.

It is ugly as hell, but if anyone has better suggestions I would be happy to "hear" them.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!