我正在使用Firebase实时数据库使用材料设计卡视图填充回收站视图,以显示不同类别的业务。
在我的导航抽屉里,我有一个类别列表,我将在我的区域中显示不同类型的业务。截至目前,我只有一个片段可以膨胀视图,以确保在我访问其他片段之前一切正常nav categories,这里是Firebase数据库的屏幕截图database
当我点击“会计/财务”片段时,我从Android工作室看到显示java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
这将我带到我的Recyclerview Adapter类中的getItemCount()
。它返回null,因为Model类为null。如果我硬编码10之类的数字以便在getItemCount()
中返回,那么onBindViewHolder()
中的错误会在我从商家列表中获得位置时出错。这是我认为与此问题有关的代码。将删除一些代码以使其尽可能简洁。
这是我从Firebase数据库获取数据并尝试将数据分配给要从Recyclerview读取的Businesses.java模型类的主要活动。
public class HomeScreenActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener
{
private List<Businesses> mBusinesses = new ArrayList<>();
private RecyclerViewAdapter mRecyclerViewAdapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
DatabaseReference database = FirebaseDatabase.getInstance().getReference().child("accounting");
database.addChildEventListener(new ChildEventListener()
{
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s)
{
Businesses model = dataSnapshot.getValue(Businesses.class);
mBusinesses.add(model);
}
});
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item)
{
int id = item.getItemId();
displaySelectedScreen(id);
item.setCheckable(true);
setTitle(item.getTitle());
return true;
}
private void displaySelectedScreen(int itemId)
{
Fragment fragment = null;
switch (itemId)
{
case R.id.nav_accounting_finance:
fragment = new AccountingFinanceFrag();
break;
}
if (fragment != null)
{
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment_container, fragment);
ft.commit();
}
}
}
这是我的Model类,当从“accounting / finance”片段调用recyclerview时返回null
@IgnoreExtraProperties
public class Businesses
{
public String Name;
public String About;
public String Picture;
public long Phone;
public Businesses()
{
}
public Businesses(String name, String about, String picture)
{
Name = name;
About = about;
Picture = picture;
}
}
这是我的Recyclerview Adapter类,我获取了应该拥有Firebase数据库数据的null Model对象。
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.RecViewHolder>
{
private Context mContext;
private List<Businesses> mBusinesses;
public RecyclerViewAdapter(Context context, List<Businesses> businesses)
{
mContext = context;
mBusinesses = businesses;
}
@Override
public RecViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.card, parent, false);
return new RecViewHolder(view);
}
@Override
public void onBindViewHolder(RecViewHolder holder, int position)
{
Businesses businesses = mBusinesses.get(position);
holder.mName.setText(businesses.Name);
holder.mAbout.setText(businesses.About);
holder.bindGalleryItem(businesses);
}
@Override
public int getItemCount()
{
return mBusinesses.size();
}
public class RecViewHolder extends RecyclerView.ViewHolder
{
private CardView mCardView;
private Businesses mBusinesses;
private ImageView mImageView;
private TextView mName, mAbout;
RecViewHolder(View itemView)
{
super(itemView);
mCardView = (CardView) itemView.findViewById(R.id.card_view);
mImageView = (ImageView) itemView.findViewById(R.id.buisness_image);
mName = (TextView) itemView.findViewById(R.id.business_name);
mAbout = (TextView) itemView.findViewById(R.id.buisness_about);
}
public void bindGalleryItem(Businesses businesses)
{
mBusinesses = businesses;
Glide.with(mContext)
.load(businesses.Picture)
.placeholder(R.mipmap.ic_launcher)
.diskCacheStrategy(DiskCacheStrategy.RESULT)
.override(200, 200)
.into(mImageView);
}
}
}
这是Recyclerview被夸大的片段。
public class AccountingFinanceFrag extends Fragment
{
private List<Businesses> mBusinesses;
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private TextView mNameText, mAboutText;
private ImageView mImageView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.fragment_category, container, false);
mNameText = (TextView) v.findViewById(R.id.business_name);
mAboutText = (TextView) v.findViewById(R.id.buisness_about);
mImageView = (ImageView) v.findViewById(R.id.buisness_image);
mRecyclerView = (RecyclerView) v.findViewById(R.id.fragment_recyclerview);
mAdapter = new RecyclerViewAdapter(getContext(), mBusinesses );
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
return v;
}
}
数据库的JSON文件
{
"accounting" : {
"buisness_0" : {
"About" : "dummy text",
"Address" : "123 test ave test, test 00000",
"Email" : "test@example.com",
"Name" : "Test test",
"Owner" : "Test owner",
"Phone" : 4120000000,
"Picture" : "add link"
},
"business_1" : {
"About" : "dummy text",
"Address" : "123 test ave test, test 00000",
"Email" : "test@example.com",
"Name" : "Test test",
"Owner" : "Test owner",
"Phone" : 4120000000,
"Picture" : "add link"
}
}
}
答案 0 :(得分:0)
mBusinesses
和片段中的mBusinesses
是不同的事情,而这就是发生混乱的地方。
在您的活动中,您初始化并向mBusinesses
添加条目,但在创建RecyclerView时,您将传递Fragment的mBusinesses
对象,该对象尚未初始化或添加了任何数据,因此{ {1}}!
要做到这一点,片段需要将数据提供给RecyclerView,可能是通过加载Fragment中的数据,或者将数据传递给Fragment或其他方式。