Android 二进制 XML 文件第 17 行:由 UnsupportedOperationException 引起的错误膨胀类 <unknown>:无法转换为维度:type=0x2

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21021127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:50:07  来源:igfitidea点击:

Binary XML file line #17: Error inflating class <unknown> caused by UnsupportedOperationException: Can't convert to dimension: type=0x2

androidbaseadapter

提问by Eman87

I`ve been trying to run my app (android) but it crashes when the following Exception

我一直在尝试运行我的应用程序(android),但是在出现以下异常时它崩溃了

[LogCat]

[日志猫]

  FATAL EXCEPTION: main
 android.view.InflateException: Binary XML file line #17: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:513)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
at emy.dt4it.healthytips.NavDrawerListAdapter.getView(NavDrawerListAdapter.java:46)
at android.widget.AbsListView.obtainView(AbsListView.java:1315)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1198)
at android.widget.ListView.onMeasure(ListView.java:1109)
at android.view.View.measure(View.java:8171)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:578)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:362)
at android.view.View.measure(View.java:8171)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
at com.slidinglayer.SlidingLayer.onMeasure(SlidingLayer.java:1077)
at android.view.View.measure(View.java:8171)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:578)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:362)
at android.view.View.measure(View.java:8171)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
at android.view.View.measure(View.java:8171)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
at android.view.View.measure(View.java:8171)
at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.reflect.InvocationTargetException
at android.widget.TextView.<init>(TextView.java:321)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
at android.view.LayoutInflater.createView(LayoutInflater.java:500)
... 37 more
 Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x2
at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:463)
at android.view.View.<init>(View.java:2036)
at android.widget.TextView.<init>(TextView.java:327)
... 41 more
01-09 11:38:22.923: I/Process(15758): Sending signal. PID: 15758 SIG: 9

and the Exception in that line

以及该行中的异常

     convertView = mInflater.inflate(R.layout.drawer_list_item, null);

in That class: [NavDrawerListAdapter.java]

在该类中:[NavDrawerListAdapter.java]

 package emy.dt4it.healthytips;
 import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
 import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class NavDrawerListAdapter extends BaseAdapter {

private Context context;
private ArrayList<NavDrawerItem> navDrawerItems;

public NavDrawerListAdapter(Context context, ArrayList<NavDrawerItem> navDrawerItems){
    this.context = context;
    this.navDrawerItems = navDrawerItems;
}

@Override
public int getCount() {
    return navDrawerItems.size();
}

@Override
public Object getItem(int position) {       
    return navDrawerItems.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater)
                context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.drawer_list_item, null);
    }

    ImageView imgIcon = (ImageView) convertView.findViewById(R.id.icon);
    TextView txtTitle = (TextView) convertView.findViewById(R.id.title);
    TextView txtCount = (TextView) convertView.findViewById(R.id.counter);

    Typeface tf = Typeface.createFromAsset(context.getAssets(), "fonts/bauhausm_0.ttf");
    txtTitle.setTypeface(tf);
    txtCount.setTypeface(tf);

    imgIcon.setImageResource(navDrawerItems.get(position).getIcon());        
    txtTitle.setText(navDrawerItems.get(position).getTitle());

    // displaying count
    // check whether it set visible or not
    if(navDrawerItems.get(position).getCounterVisibility()){
        txtCount.setText(navDrawerItems.get(position).getCount());
    }else{
        // hide the counter view
        txtCount.setVisibility(View.GONE);
    }

    return convertView;
}

}

and the layout [drawer_list_item]

和布局 [drawer_list_item]

<RelativeLayout android:layout_width="match_parent"
    android:layout_height="48dp"
    android:background="@drawable/list_selector">
    <ImageView android:id="@+id/icon"   
        android:layout_width="25dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="12dp"
        android:layout_marginRight="12dp"
        android:src="@drawable/menu_tip"
        android:layout_centerVertical="true"/>
    <TextView android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_toRightOf="@id/icon" 
        android:minHeight="?android:attr/listPreferredItemHeightSmall"
        android:textAppearance="?android:attr/textAppearanceListItemSmall" 
        android:textColor="#4f4f4f" 
        android:textSize="18sp" 
        android:text="try" 
        android:gravity="center_vertical"
        android:paddingRight="40dp"/>
    <TextView android:id="@+id/counter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true" 
        android:text="2" 
        android:layout_marginRight="8dp" 
        android:textColor="#959595"/>
</RelativeLayout>

Hope anyone could help me. Thanks in advance.

希望任何人都可以帮助我。提前致谢。

回答by Aleksey Makarov

error in xml
check that you are in line 17 of your submission.

xml 中的错误
检查您是否在提交的第 17 行。

android:attr/textAppearanceListItemSmall
if the error here is api level change in the manifest

android:attr/textAppearanceListItemSmall
如果这里的错误是清单中的 api 级别更改

回答by molnarm

The innermost exception means that one of your textviews have invalid dimensions declared. Check the SDK level you are using, because android:attr/listPreferredItemHeightSmalland android:attr/textAppearanceListItemSmallare available from API level 14 and up.

最里面的异常意味着您的文本视图之一声明了无效的尺寸。检查您正在使用的 SDK 级别,因为android:attr/listPreferredItemHeightSmallandroid:attr/textAppearanceListItemSmall可用于 API 级别 14 及更高版本。

(added from my comment which fixed the issue)

(从我解决问题的评论中添加)

回答by Williaan Lopes

I found the same error on the same line, and it took two days to identify what the error was.

我在同一行上发现了同样的错误,花了两天时间才确定错误是什么。

The error was simply because I was trying to use one: android:background

错误只是因为我试图使用一个: android:background

Rather than: app:srcCompat

而不是: app:srcCompat

in an SVG file.

在 SVG 文件中。

In your case,

在你的情况下,

<ImageView android:id="@+id/icon"   
        android:layout_width="25dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="12dp"
        android:layout_marginRight="12dp"
        android:src="@drawable/menu_tip"   <-- the error is here
        android:layout_centerVertical="true"/>

the error also appears if you are using: android:src=" "

如果您正在使用,也会出现该错误: android:src=" "

I hope this helps.

我希望这有帮助。