RecyclerView错误膨胀textView

时间:2016-12-19 06:19:33

标签: java android android-recyclerview recycler-adapter

我刚刚开始研究recyclerView,并在我收到此错误时尝试使用自定义对象实现基本操作

    E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.sriran.recyclerview, PID: 3592
              android.view.InflateException: Binary XML file line #6: Binary XML file line #6: Error inflating class TextView
              Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class TextView
              Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 2: TypedValue{t=0x2/d=0x7f0100d2 a=-1}
                  at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:716)
                  at android.widget.TextView.<init>(TextView.java:1088)
                  at android.widget.TextView.<init>(TextView.java:704)
                  at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:62)
                  at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:58)
                  at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:103)
                  at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1021)
                  at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1080)
                  at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:47)
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:769)
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
                  at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
                  at com.example.sriran.recyclerview.WordsAdapter.onCreateViewHolder(WordsAdapter.java:32)
                  at com.example.sriran.recyclerview.WordsAdapter.onCreateViewHolder(WordsAdapter.java:16)
                  at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6290)
                  at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5478)
                  at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5363)
                  at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5359)
                  at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2141)
                  at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1525)
                  at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1488)
                  at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:585)
                  at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3506)
                  at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3254)
                  at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3767)
                  at android.view.View.layout(View.java:17523)
                  at android.view.ViewGroup.layout(ViewGroup.java:5612)
                  at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
                  at android.view.View.layout(View.java:17523)
                  at android.view.ViewGroup.layout(ViewGroup.java:5612)
                  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                  at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                  at android.view.View.layout(View.java:17523)
                  at android.view.ViewGroup.layout(ViewGroup.java:5612)
                  at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:437)
                  at android.view.View.layout(View.java:17523)
                  at android.view.ViewGroup.layout(ViewGroup.java:5612)
                  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                  at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                  at android.view.View.layout(View.java:17523)
                  at android.view.ViewGroup.layout(ViewGroup.java:5612)
                  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
                  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                  at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                  at android.view.View.layout(View.java:17523)
                  at android.view.ViewGroup.layout(ViewGroup.java:5612)
       E/AndroidRuntime:     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                  at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                  at com.android.internal.policy.DecorView.onLayout(DecorView.java:724)
                  at android.view.View.layout(View.java:17523)
                  at android.view.ViewGroup.layout(ViewGroup.java:5612)
                  at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2342)
                  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2069)
                  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246)
                  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6301)
                  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
                  at android.view.Choreographer.doCallbacks(Choreographer.java:683)
                  at android.view.Choreographer.doFrame(Choreographer.java:619)
                  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
                  at android.os.Handler.handleCallback(Handler.java:751)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6077)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
    E/EGL_emulation: tid 3607: eglSurfaceAttrib(1146): error 0x3009 (EGL_BAD_MATCH)
    W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xafaff140, error=EGL_BAD_MATCH

我尝试了一些东西,但没有工作。我不知道我哪里出错了。 这是我的适配器代码

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

        private ArrayList<Words> mWords;
        private Context mContext;

        public WordsAdapter(Context context , ArrayList<Words> words)
        {
            mWords = words;
            mContext = context;
        }
        private Context getContext(){return  mContext;}

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            Context context = parent.getContext();
            LayoutInflater inflater = LayoutInflater.from(getContext());
            View view = inflater.inflate(R.layout.list_item , parent ,false);
            ViewHolder viewHolder = new ViewHolder(view);

            return viewHolder;
        }

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

            Words word = mWords.get(position);

            TextView title = holder.title;
            title.setText(word.getTitle());

            TextView subtitle = holder.subtitle;
            subtitle.setText(word.getSubtitle());

        }

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

        public static class ViewHolder extends RecyclerView.ViewHolder{

            public TextView  title , subtitle;

            public ViewHolder(View itemView) {
                super(itemView);
                title = (TextView) itemView.findViewById(R.id.title);
                subtitle = (TextView) itemView.findViewById(R.id.subtitle);
            }
        }
   }

这是我的MainActivity java文件

    private static final String[] title = { "ABCD" , "EFGH" , "IJKL" , "MNOP", "QRST" , "UVW" , "XYZ"};
    private static final String[] subtitle = {"abcd" , "efgh" , "ijkl" , "mnop" , "qrst", "uvw" , "xyz"};
    private ArrayList<Words> words;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        words = new ArrayList<>();
        for (int i=0 ; i<title.length ; i++)
        {
            words.add(new Words(title[i] , subtitle[i]));
        }

        RecyclerView rvWords = (RecyclerView) findViewById(R.id.rv_words);

        WordsAdapter adapter = new WordsAdapter(MainActivity.this , words);

        rvWords.setLayoutManager(new LinearLayoutManager(this));



        rvWords.setAdapter(adapter);

    }
 }

这是我的布局项xml代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="?attr/titleTextAppearance"
            android:textStyle="bold"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/subtitle"
            android:textSize="?attr/subtitleTextAppearance"
            />

    </LinearLayout>

和recycler查看xml代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.sriran.recyclerview.MainActivity">

<android.support.v7.widget.RecyclerView
    android:id="@+id/rv_words"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

请帮我弄清楚我哪里出错了

4 个答案:

答案 0 :(得分:1)

您可以删除代码以从父textize中提取而不是使用

android:textSize="16dp"

因为系统无法获得 subtitleTextAppearance

答案 1 :(得分:0)

从列表项中删除此android:textSize="?attr/titleTextAppearance"或更正它以解决您的问题。

答案 2 :(得分:0)

将项目布局更改为

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="10sp"
    android:textStyle="bold"
    />
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/subtitle"
    android:textSize="10sp"
    /> 

我认为编译器能够找到“?attr / titleTextAppearance”和“?attr / subtitleTextAppearance”属性,只需这样就试试吧

答案 3 :(得分:0)

从适配器项目布局中删除此android:textSize="?attr/titleTextAppearance"并根据需要提供文字大小。(此处我使用16sp14sp作为示例)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:textStyle="bold"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/subtitle"
            android:textSize="14sp"/>
    </LinearLayout>