android - 二进制XML文件行#2:错误膨胀类<unknown>

时间:2015-08-09 20:56:00

标签: android baseadapter

当我在手机上运行我的应用程序(API 21)时,一切正常,但是当我在平板电脑上运行它(API 19)时,它崩溃并出现以下错误:

[logcat的]

 Process: com.tsirigotis.myradio, PID: 6183
    android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
            at android.view.LayoutInflater.createView(LayoutInflater.java:626)
            at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
            at android.view.LayoutInflater.onCreateView(LayoutInflater.java:675)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:700)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:470)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
            at com.tsirigotis.myradio.library.Adapter_NavDrawerList.getView(Adapter_NavDrawerList.java:47)
            at android.widget.AbsListView.obtainView(AbsListView.java:2724)
            at android.widget.ListView.makeAndAddView(ListView.java:1811)
            at android.widget.ListView.fillDown(ListView.java:697)
            at android.widget.ListView.fillFromTop(ListView.java:763)
            at android.widget.ListView.layoutChildren(ListView.java:1627)
            at android.widget.AbsListView.onLayout(AbsListView.java:2549)
            at android.view.View.layout(View.java:15746)
            at android.view.ViewGroup.layout(ViewGroup.java:4867)
            at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
            at android.view.View.layout(View.java:15746)
            at android.view.ViewGroup.layout(ViewGroup.java:4867)
            at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:931)
            at android.view.View.layout(View.java:15746)
            at android.view.ViewGroup.layout(ViewGroup.java:4867)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
            at android.view.View.layout(View.java:15746)
            at android.view.ViewGroup.layout(ViewGroup.java:4867)
            at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:493)
            at android.view.View.layout(View.java:15746)
            at android.view.ViewGroup.layout(ViewGroup.java:4867)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
            at android.view.View.layout(View.java:15746)
            at android.view.ViewGroup.layout(ViewGroup.java:4867)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
            at android.view.View.layout(View.java:15746)
            at android.view.ViewGroup.layout(ViewGroup.java:4867)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
            at android.view.View.layout(View.java:15746)
            at android.view.ViewGroup.layout(ViewGroup.java:4867)
            at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2356)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2069)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6630)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
            at android.view.Choreographer.doCallbacks(Choreographer.java:603)
            at android.view.Choreographer.doFrame(Choreographer.java:573)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5479)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            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

以下行发生异常:

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

异常所引用的类[Adapter_NavDrawerList.java]是:

package com.tsirigotis.myradio.library;


import java.util.ArrayList;

import com.tsirigotis.myradio.R;

import android.app.Activity;
import android.content.Context;
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 Adapter_NavDrawerList extends BaseAdapter {

    private Context context;
    private ArrayList<Object_NavDrawer> navDrawerItems;

    public Adapter_NavDrawerList(Context context, ArrayList<Object_NavDrawer> 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.item_drawer, 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);

        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;
    }

}

和布局[item_drawer.xml]

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:background="@drawable/list_selector"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <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/ic_drawer"
        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:gravity="center_vertical"
        android:paddingRight="40dp"
        android:textColor="?attr/list_item_title"
        android:textSize="14sp" />

    <TextView android:id="@+id/counter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/counter_bg"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="8dp"
        android:textColor="@color/holo_counter_text_color"/>

</RelativeLayout>

如果有人可以帮助我,我会很感激。谢谢。忘了提Minsdk 16

1 个答案:

答案 0 :(得分:0)

您是否检查过所有资源?堆栈跟踪清楚地表明某些元素无法从XML实例化,并且您没有使用任何自定义视图,因此必须是资源。