Android'InvocationTargetException'和'UnsupportedOperationException'

时间:2015-09-22 10:34:10

标签: android android-layout textview android-xml

我在OPPO X905和OPPO X907上遇到了问题,两者都是Android 4.0.3(API等级15),我没有使用任何更高级别的API,而且我在Google和SO上搜索过,都是不同的从我的问题,下面是我的xml文件和崩溃日志,提前谢谢。

xml文件:(分隔符的边距最初是7.5dp,这可能是问题吗?)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_full"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#eff0f7"
    android:baselineAligned="false"
    android:orientation="vertical" >
    <RelativeLayout
        android:id="@+id/item_first_half"
        android:layout_width="match_parent"
        android:layout_height="36dp"
        android:orientation="vertical" >
        <View
            android:id="@+id/first_path"
            android:layout_width="4dp"
            android:layout_height="4dp"
            android:layout_marginLeft="40dp"
            android:layout_marginStart="40dp"
            android:background="@drawable/selector_car_action_path" />
        <ImageView
            android:id="@+id/im_car_direction_icon"
            android:layout_width="28dp"
            android:layout_height="28dp"
            android:layout_gravity="top"
            android:layout_marginLeft="28dp"
            android:layout_marginStart="28dp"
            android:layout_marginTop="4dp"
            android:background="@drawable/selector_car_action_background"
            android:padding="2dp"
            android:src="@drawable/selector_navi_icon_63" />
        <View
            android:id="@+id/second_path"
            android:layout_width="4dp"
            android:layout_height="4dp"
            android:layout_below="@id/im_car_direction_icon"
            android:layout_marginLeft="40dp"
            android:layout_marginStart="40dp"
            android:background="@drawable/selector_car_action_path" />
        <TextView
            android:id="@+id/tv_direction"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginEnd="15dp"
            android:layout_marginLeft="68dp"
            android:layout_marginRight="15dp"
            android:layout_marginStart="68dp"
            android:singleLine="true"
            android:text=""
            android:textColor="@drawable/selector_car_route_turn_text"
            android:textSize="16sp" />
    </RelativeLayout>
    <RelativeLayout
        android:id="@+id/item_second_half"
        android:layout_width="match_parent"
        android:layout_height="32dp" >
        <View
            android:id="@+id/third_path"
            android:layout_width="4dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="40dp"
            android:layout_marginStart="40dp"
            android:background="@drawable/selector_car_action_path" />
        <TextView
            android:id="@+id/tv_road_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="68dp"
            android:layout_marginStart="68dp"
            android:layout_marginTop="4dp"
            android:singleLine="true"
            android:text=""
            android:textColor="@drawable/selector_car_route_desc"
            android:textSize="16sp" />
        <TextView
            android:id="@+id/divider_for_road_and_distance"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginEnd="7dp"
            android:layout_marginLeft="7dp"
            android:layout_marginRight="7dp"
            android:layout_marginStart="7dp"
            android:layout_marginTop="4dp"
            android:layout_toEndOf="@id/tv_road_name"
            android:layout_toRightOf="@id/tv_road_name"
            android:singleLine="true"
            android:text="|"
            android:textColor="@drawable/selector_car_route_desc"
            android:textSize="16sp" />
        <TextView
            android:id="@+id/tv_ride_distance"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginTop="4dp"
            android:layout_toEndOf="@id/divider_for_road_and_distance"
            android:layout_toRightOf="@id/divider_for_road_and_distance"
            android:singleLine="true"
            android:text=""
            android:textColor="@drawable/selector_car_route_desc"
            android:textSize="16sp" />
        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:layout_alignParentBottom="true"
            android:layout_marginEnd="15dp"
            android:layout_marginLeft="68dp"
            android:layout_marginRight="15dp"
            android:layout_marginStart="68dp"
            android:background="#dee1f0" />
    </RelativeLayout>
</LinearLayout>

drawable / selector_car_route_desc文件是

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_selected="true" android:color="@color/color_999999"/>
    <item android:state_selected="false" android:color="@color/color_333333"/>

</selector>

崩溃日志:

android.view.InflateException: Binary XML file line #106: Error inflating class <unknown>
android.view.LayoutInflater.createView(LayoutInflater.java:606)
com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
android.view.LayoutInflater.inflate(LayoutInflater.java:489)
android.view.LayoutInflater.inflate(LayoutInflater.java:396)
android.view.LayoutInflater.inflate(LayoutInflater.java:352)
com.tencent.map.ama.route.ui.view.e.<init>(CarRouteShowItem.java:78)
com.tencent.map.ama.route.ui.view.f.a(CarRouteShowView.java:100)
com.tencent.map.ama.route.ui.view.d.a(CarRouteDetailView.java:37)
com.tencent.map.ama.route.ui.MapStateCarRoute.e(MapStateCarRoute.java:418)
com.tencent.map.ama.route.ui.MapStateCarRoute$1.run(MapStateCarRoute.java:255)
android.os.Handler.handleCallback(Handler.java:605)
android.os.Handler.dispatchMessage(Handler.java:92)
android.os.Looper.loop(Looper.java:137)
android.app.ActivityThread.main(ActivityThread.java:4476)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:511)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:808)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:575)
dalvik.system.NativeStart.main(Native Method)
cause by:
java.lang.reflect.InvocationTargetException: null
java.lang.reflect.Constructor.constructNative(Native Method)
java.lang.reflect.Constructor.newInstance(Constructor.java:417)
android.view.LayoutInflater.createView(LayoutInflater.java:586)
com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
android.view.LayoutInflater.inflate(LayoutInflater.java:489)
android.view.LayoutInflater.inflate(LayoutInflater.java:396)
android.view.LayoutInflater.inflate(LayoutInflater.java:352)
com.tencent.map.ama.route.ui.view.e.<init>(CarRouteShowItem.java:78)
com.tencent.map.ama.route.ui.view.f.a(CarRouteShowView.java:100)
com.tencent.map.ama.route.ui.view.d.a(CarRouteDetailView.java:37)
com.tencent.map.ama.route.ui.MapStateCarRoute.e(MapStateCarRoute.java:418)
com.tencent.map.ama.route.ui.MapStateCarRoute$1.run(MapStateCarRoute.java:255)
android.os.Handler.handleCallback(Handler.java:605)
android.os.Handler.dispatchMessage(Handler.java:92)
android.os.Looper.loop(Looper.java:137)
android.app.ActivityThread.main(ActivityThread.java:4476)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:511)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:808)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:575)
dalvik.system.NativeStart.main(Native Method)
cause by:
java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x12
android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:463)
android.view.View.<init>(View.java:2849)
android.widget.TextView.<init>(TextView.java:499)
android.widget.TextView.<init>(TextView.java:492)
java.lang.reflect.Constructor.constructNative(Native Method)
java.lang.reflect.Constructor.newInstance(Constructor.java:417)
android.view.LayoutInflater.createView(LayoutInflater.java:586)
com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
android.view.LayoutInflater.inflate(LayoutInflater.java:489)
android.view.LayoutInflater.inflate(LayoutInflater.java:396)
android.view.LayoutInflater.inflate(LayoutInflater.java:352)
com.tencent.map.ama.route.ui.view.e.<init>(CarRouteShowItem.java:78)
com.tencent.map.ama.route.ui.view.f.a(CarRouteShowView.java:100)
com.tencent.map.ama.route.ui.view.d.a(CarRouteDetailView.java:37)
com.tencent.map.ama.route.ui.MapStateCarRoute.e(MapStateCarRoute.java:418)
com.tencent.map.ama.route.ui.MapStateCarRoute$1.run(MapStateCarRoute.java:255)
android.os.Handler.handleCallback(Handler.java:605)
android.os.Handler.dispatchMessage(Handler.java:92)
android.os.Looper.loop(Looper.java:137)
android.app.ActivityThread.main(ActivityThread.java:4476)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:511)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:808)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:575)
dalvik.system.NativeStart.main(Native Method)

CarRouteShowItem.java:78的代码是这样的:

View mView = LayoutInflater.from(mContext).inflate(R.layout.car_route_detail_item, null);

CarRouteShowItem不是View的子代,其代码如下:

public class CarRouteShowItem {

    private Context mContext;

    private View mView;

    public CarRouteShowItem(Context context) {
        mContext = context;

        // mView is used by caller 
        mView = LayoutInflater.from(mContext).inflate(R.layout.car_route_detail_item, null);

        // some view finds 
    }

        // some other methods related with specific logic
}

图形布局可以在Eclipse中从API级别8预览到API级别23,但是当我在API级别15的Android Studio中预览时,它会显示错误消息:无法解析资源@style / Widget.TextView ,其他API级别没问题,但我没有使用TextView的样式。

2 个答案:

答案 0 :(得分:4)

你需要的只是一个额外的眼睛 :-)

enter image description here

 android:layout_height="1px"
希望你现在能看到。您的第二个RelativeLayout中姓item_second_half <View的孩子将其更改为

android:layout_height="1dp"

让我尽快回顾

答案 1 :(得分:3)

我认为问题出现在App风格的@style /引用中。尝试删除所有这些(重置为默认值)并逐个添加,直到崩溃发生。这样您就可以找到具有错误属性的样式。

如果您看到日志错误,您将看到java.lang.UnsupportedOperationException发生InvocationTargetException:无法转换为维度:type = 0x12

更新

我注意到你的xml文件有些不好,你使用的文字颜色是“可绘制的”,你必须使用颜色xml文件(example

再次更新

经过多次搜索后我发现这个file如果你看到499和492行(出现在logerror上),你会发现它可能找不到textview的样式