为啥 recyclerview$adapter 在片段中为空
Posted
技术标签:
【中文标题】为啥 recyclerview$adapter 在片段中为空【英文标题】:why recyclerview$adapter is null in fragment为什么 recyclerview$adapter 在片段中为空 【发布时间】:2021-11-19 15:05:12 【问题描述】:这是我的错误代码......
我正在尝试创建一个片段来读取联系人,但是当我运行它时它崩溃了......
但这不会发生在活动中......
java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView$Adapter)' on a null object reference
at com.*******"".****.manuTwo.ContactsFragment.GetData(ContactsFragment.java:82)
at com.**********.******manuTwo.ContactsFragment.onCreate(ContactsFragment.java:42)
at androidx.fragment.app.Fragment.performCreate(Fragment.java:2949)
at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:475)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:278)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
at androidx.fragment.app.FragmentManager.execSingleAction(FragmentManager.java:1971)
at androidx.fragment.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:311)
at androidx.fragment.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:249)
at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1244)
at androidx.viewpager.widget.ViewPager.setCurrentItemInternal(ViewPager.java:669)
at androidx.viewpager.widget.ViewPager.setCurrentItemInternal(ViewPager.java:631)
at androidx.viewpager.widget.ViewPager.setCurrentItem(ViewPager.java:612)
at com.google.android.material.tabs.TabLayout$ViewPagerOnTabSelectedListener.onTabSelected(TabLayout.java:3401)
at com.google.android.material.tabs.TabLayout.dispatchTabSelected(TabLayout.java:1856)
at com.google.android.material.tabs.TabLayout.selectTab(TabLayout.java:1849)
at com.google.android.material.tabs.TabLayout.selectTab(TabLayout.java:1809)
at com.google.android.material.tabs.TabLayout$Tab.select(TabLayout.java:2249)
at com.google.android.material.tabs.TabLayout$TabView.performClick(TabLayout.java:2438)
at android.view.View.performClickInternal(View.java:7284)
at android.view.View.access$3600(View.java:821)
at android.view.View$PerformClick.run(View.java:28053)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:226)
at android.app.ActivityThread.main(ActivityThread.java:7592)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
【问题讨论】:
请包含您的一些被此异常引用的代码,以便我们验证问题出在哪里。 欢迎来到 ***!请参阅how to ask a good question 上的指南以及在需要时如何提供minimal reproducible example 以便人们可以尝试帮助您解决问题。在提出问题之前,您通常需要自己做功课和研究。 【参考方案1】:这是我的适配器:
package com.myphone.contact.adapter.UserContactsAdapter;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.imageview.ShapeableImageView;
import com.myphone.contact.R;
public class UserContactAdapter extends RecyclerView.ViewHolder
public ShapeableImageView ImageProfile;
public TextView usersName, userPhone, userStatus, hiFriend;
public UserContactAdapter(@NonNull View itemView)
super(itemView);
ImageProfile = (ShapeableImageView) itemView.findViewById(R.id.user_image_contact_friend);
usersName = (TextView) itemView.findViewById(R.id.user_Name_contact_friend);
userPhone = (TextView) itemView.findViewById(R.id.user_Phone_contact_friend);
userStatus = (TextView) itemView.findViewById(R.id.status_user_contact_friend);
hiFriend = (TextView) itemView.findViewById(R.id.hi_my_friend);
【讨论】:
以上是关于为啥 recyclerview$adapter 在片段中为空的主要内容,如果未能解决你的问题,请参考以下文章
java 在RecyclerView Adapter onCreateViewHolder()获取查看位置
RecyclerView.Adapter.notifyItemMoved(0,1) 滚动屏幕