当我调用 setAdapter() 到 recycleView 时,我的 Android 应用程序意外退出

Posted

技术标签:

【中文标题】当我调用 setAdapter() 到 recycleView 时,我的 Android 应用程序意外退出【英文标题】:My Android app quit unexpectedly when I invoke setAdapter() to the recycleView 【发布时间】:2021-07-28 13:57:43 【问题描述】:

我使用安卓机房来保存我的数据,我可以成功地从数据库中获取数据。但是,当我调用 setAdapter() 时,应用程序崩溃了。 这是我的适配器代码

public class QueryAdapter extends RecyclerView.Adapter<QueryAdapter.MyViewHolder> 
     List<Student> localData;

    public  class  MyViewHolder extends RecyclerView.ViewHolder

        TextView id,name,sex,address,major;
        public MyViewHolder(@NonNull View itemView) 
            super(itemView);
            id = itemView.findViewById(R.id.show_id);
            name = itemView.findViewById(R.id.show_name);
            sex = itemView.findViewById(R.id.show_sex);
            address = itemView.findViewById(R.id.show_address);
            major = itemView.findViewById(R.id.show_major);
        
    

    public QueryAdapter(List<Student> students)
        this.localData = students;
    
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.query_item, parent, false);

        return new MyViewHolder(view);
    

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) 
        holder.id.setText(localData.get(position).id);
        holder.major.setText(localData.get(position).major);
        holder.sex.setText(localData.get(position).sex);
        holder.address.setText(localData.get(position).address);
        holder.name.setText(localData.get(position).name);

    


    @Override
    public int getItemCount() 
        return this.localData.size();
    

这是我使用recycleView的代码

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        localData = studentDao.loadAllStudent();
        QueryAdapter myAdapter = new QueryAdapter(localData);
        recyclerView.setAdapter(myAdapter);

日志是这样的,它说我的onBindViewHolder()有问题,但我不知道为什么它不能工作。

2021-05-06 17:31:35.495 25980-25980/com.example.homework_2021_5_6 E/androidRuntime: FATAL EXCEPTION: main
    Process: com.example.homework_2021_5_6, PID: 25980
    android.content.res.Resources$NotFoundException: String resource ID #0x1
        at android.content.res.Resources.getText(Resources.java:444)
        at android.widget.TextView.setText(TextView.java:6436)
        at com.example.homework_2021_5_6.adapter.QueryAdapter.onBindViewHolder(QueryAdapter.java:46)
        at com.example.homework_2021_5_6.adapter.QueryAdapter.onBindViewHolder(QueryAdapter.java:16)
        at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
        at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6012)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6279)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
        at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
        at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
        at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
        at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
        at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
        at android.view.View.layout(View.java:23042)
        at android.view.ViewGroup.layout(ViewGroup.java:6419)
        at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1915)
        at android.view.View.layout(View.java:23042)
        at android.view.ViewGroup.layout(ViewGroup.java:6419)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at android.view.View.layout(View.java:23042)
        at android.view.ViewGroup.layout(ViewGroup.java:6419)
        at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:446)
        at android.view.View.layout(View.java:23042)
        at android.view.ViewGroup.layout(ViewGroup.java:6419)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at android.view.View.layout(View.java:23042)
        at android.view.ViewGroup.layout(ViewGroup.java:6419)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
        at android.view.View.layout(View.java:23042)
        at android.view.ViewGroup.layout(ViewGroup.java:6419)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at com.android.internal.policy.DecorView.onLayout(DecorView.java:820)
        at android.view.View.layout(View.java:23042)
        at android.view.ViewGroup.layout(ViewGroup.java:6419)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3786)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3227)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2182)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8730)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1352)
        at android.view.Choreographer.doCallbacks(Choreographer.java:1149)
        at android.view.Choreographer.doFrame(Choreographer.java:1049)
        at android.view.Choreographer$FrameHandler.handleMessage(Choreographer.java:1275)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:233)
        at android.app.ActivityThread.main(ActivityThread.java:8010)
2021-05-06 17:31:35.495 25980-25980/com.example.homework_2021_5_6 E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:631)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:978)
2021-05-06 17:31:35.554 25980-25980/com.example.homework_2021_5_6 I/Process: Sending signal. PID: 25980 SIG: 9

【问题讨论】:

分享日志,以便人们缩小原因。 很难说哪一行出错了,但显然视图 ID 不正确。发布您的布局(对于 viewHolder),您是否对此进行了调试? findViewById 是返回引用还是 null?无关:show_sexsex 应该是 show_gendergender,除非这个应用程序是针对成年人和他们的性生活 :) 【参考方案1】:

关键是您尝试分配给 TextView 的资源不存在。这可能是由许多因素造成的:

    您正在尝试分配不存在的内容:textView.setText = R.string.does_not_exist 您正在尝试分配一个数字文本:
someTextView.setText(1)

^ Android 会认为 1 是资源 id 并会尝试找到它。

相反,您应该转换它或传递一个字符串

someTextView.setText("1")

or

someTextView.setText(1.toString())

很可能是其中之一:

setText(localData.get(position).xxxx)

里面有一个数字,你实际调用的 setText(...) 方法就是这个:

    /**
     * Sets the text to be displayed using a string resource identifier.
     *
     * @param resid the resource identifier of the string resource to be displayed
     *
     * @see #setText(CharSequence)
     *
     * @attr ref android.R.styleable#TextView_text
     */
    @android.view.RemotableViewMethod
    public final void setText(@StringRes int resid) 
        setText(getContext().getResources().getText(resid));
        mTextSetFromXmlOrResourceId = true;
        mTextId = resid;
    

而不是

  public final void setText(CharSequence text) 
        setText(text, mBufferType);
    

【讨论】:

非常感谢。我从您的回答中学到了很多东西。【参考方案2】:

在你的 onBindViewHolder() 方法中:

而不是,

holder.id.setText(localData.get(position).id);

用途:

holder.id.setText(Integer.toString(localData.get(position).id));

你应该访问这个https://***.com/a/26001905/12660050

【讨论】:

以上是关于当我调用 setAdapter() 到 recycleView 时,我的 Android 应用程序意外退出的主要内容,如果未能解决你的问题,请参考以下文章

setAdapter“调用需要 API 级别 11(当前最低为 8):android.widget.AbsListView#setAdapter”?

从源码上分析ListView的addHeaderView和setAdapter的调用顺序

Android - 片段中的 setAdapter [重复]

setAdapter() 上的 NullPointer 异常

SetAdapter 不能作为 FirestoreRecyclerviewAdapter 的适配器应用

Android:尝试在空对象引用上调用虚拟方法“void android.widget.ListView.setAdapter(android.widget.ListAdapter)”