gpt4 book ai didi

java - RecyclerView 错误膨胀 textView

转载 作者:行者123 更新时间:2023-12-01 09:06:39 25 4
gpt4 key购买 nike

当我收到此错误时,我刚刚开始研究 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>

以及回收器 View 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>

请帮我找出哪里出错了

最佳答案

您可以删除代码以从父级 textsize 中获取,而不是使用

android:textSize="16dp"

因为系统无法获取subtitleTextAppearance

关于java - RecyclerView 错误膨胀 textView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41216791/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com