首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >android.view.InflateException二进制XML文件第0行:由于以下原因导致类<unknown>膨胀时出错: java.lang.reflect.InvocationTargetException

android.view.InflateException二进制XML文件第0行:由于以下原因导致类<unknown>膨胀时出错: java.lang.reflect.InvocationTargetException
EN

Stack Overflow用户
提问于 2018-09-28 02:59:33
回答 1查看 1.9K关注 0票数 2

android.view.InflateException二进制XML文件第0行:由以下原因导致的类膨胀时出错: java.lang.reflect.InvocationTargetException

employee_layout.xml文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardElevation="10dp"
    app:cardBackgroundColor="@color/cardview_light_background"
    >

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:backgroundSplit"
        android:padding="10dp"
        >

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/name"
            android:textColor="@android:color/black"
            android:textSize="15sp"
            android:textStyle="bold"/>
        <TextView
            android:id="@+id/type"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/type"
            android:textColor="@android:color/black"
            android:textSize="15sp"
            android:textStyle="normal"/>

    </LinearLayout>

</android.support.v7.widget.CardView>

适配器类:

class LoadingViewHolder extends RecyclerView.ViewHolder{

    public ProgressBar progressBar;

    public LoadingViewHolder(View itemView) {
        super( itemView );
        progressBar = (ProgressBar)itemView.findViewById( R.id.progressBar );
    }

}

class EmpViewHolder extends RecyclerView.ViewHolder{

    public TextView name , type;

    public EmpViewHolder(View itemView) {
        super( itemView );
        name = (TextView)itemView.findViewById( R.id.fname);
        type = (TextView)itemView.findViewById( R.id.lname );
    }
}

public class MyAdaptor extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final int view_type_item = 0 , view_type_loading = 1;
    ILoadMore iLoadMore;
    boolean isLoading;
    Activity activity;
    List<Employee> employees;
    int visibleThreshold = 5;
    int lastVisibleItem , totalItemCount;


    public MyAdaptor(RecyclerView recyclerView , Activity activity, List<Employee> employees) {
        this.activity = activity;
        this.employees = employees;

        final LinearLayoutManager linearLayoutManager = (LinearLayoutManager)recyclerView.getLayoutManager();
        recyclerView.addOnScrollListener( new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled( recyclerView, dx, dy );
                totalItemCount = linearLayoutManager.getItemCount();
                lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();

                if(!isLoading && totalItemCount <= (lastVisibleItem + visibleThreshold))
                {
                    if (iLoadMore != null)
                        iLoadMore.onLoadMore();
                    isLoading = true;
                }

            }
        } );
    }

    @Override
    public int getItemViewType(int position) {
        return employees.get(position) == null ? view_type_loading:view_type_item;
    }

    public void setiLoadMore(ILoadMore iLoadMore) {
        this.iLoadMore = iLoadMore;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == view_type_item)
        {
            View view = LayoutInflater.from( activity )
                    .inflate(R.layout.employee_layout , parent , false );
            return  new EmpViewHolder( view );
        }
        else if (viewType == view_type_loading){
            View view = LayoutInflater.from( activity )
                    .inflate( R.layout.employee_loading , parent, false );
            return new LoadingViewHolder( view );
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if (holder instanceof EmpViewHolder)
        {
            Employee employee = employees.get(position);
            EmpViewHolder viewHolder = (EmpViewHolder) holder;
            viewHolder.name.setText( employees.get(position).getFname() );
            viewHolder.type.setText( employees.get( position ).getType() );
        }
        else if (holder instanceof  LoadingViewHolder)
        {
            LoadingViewHolder loadingViewHolder = (LoadingViewHolder)holder;
            loadingViewHolder.progressBar.setIndeterminate( true );
        }
    }

    @Override
    public int getItemCount() {
        return employees.size();
    }

    public void setLoaded(){
        isLoading = false;
    }
}

Adpoter.java结束

-堆栈跟踪

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.user.foodorderingapp, PID: 22889
                  android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class <unknown>
                  Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class <unknown>
                  Caused by: java.lang.reflect.InvocationTargetException
                      at java.lang.reflect.Constructor.newInstance0(Native Method)
                      at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
                      at android.view.LayoutInflater.createView(LayoutInflater.java:647)
                      at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
                      com.example.user.foodorderingapp.EmployeeFiles.MyAdaptor.onCreateViewHolder(MyAdaptor.java:91)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-28 04:08:23

android.R.attr.backgroundSplit用于拆分ActionBars (在布局底部显示额外的按钮等)。它确实定义了一个背景,但根据文档,它可以引用任何类型的资源,任何类型的主题,甚至只是一个直接的十六进制颜色字符串。

LinearLayout的背景字段只能接受颜色、颜色引用或可绘制引用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52543516

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档