java试图调用虚拟方法“void 安卓x”。回收视图。小装置。回收视图。空对象引用上的setHasFixedSize(布尔)“”
因此,基本上当我运行我的应用程序时,该应用程序不会给我任何错误。但是,当我尝试在我的应用程序中打开一个名为“查找食物捐赠”的页面时,它在日志中给了我以下错误代码:
'Attempt to invoke virtual method 'void 安卓x.recyclerview.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference' and also said that its Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void 安卓x.recyclerview.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference:
在com。实例多年发展目标项目。发现食物的国家。onCreate(findfooddonions.java:66)
下面是Java文件中发生错误的应用程序的代码(粗体)
public class FindFoodDonations extends AppCompatActivity{
private DatabaseReference ProductsRef;
private RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find_food_donations);
ProductsRef = FirebaseDatabase.getInstance().getReference().child("Products");
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_organization_navigation);
bottomNavigationView.setSelectedItemId(R.id.nav_find_donations);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_find_donations:
return true;
case R.id.nav_organization_home:
startActivity(new Intent(getApplicationContext()
, HomeOrganization.class));
overridePendingTransition(0, 0);
return true;
case R.id.nav_organization_logout:
FirebaseAuth.getInstance().signOut();
Intent intosignup = new Intent(FindFoodDonations.this, MainActivity.class);
startActivity(intosignup);
return true;
}
return false;
}
});
recyclerView = findViewById(R.id.recycler_menu);
**recyclerView.setHasFixedSize(true);**
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
}
@Override
protected void onStart() {
super.onStart();
FirebaseRecyclerOptions<Product> options =
new FirebaseRecyclerOptions.Builder<Product>()
.setQuery(ProductsRef, Product.class)
.build();
FirebaseRecyclerAdapter<Product, ProductViewHolder> adapter =
new FirebaseRecyclerAdapter<Product, ProductViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull ProductViewHolder productViewHolder, int i, @NonNull Product product) {
productViewHolder.txtProductName.setText(product.getPname());
productViewHolder.txtProductPrice.setText("Quantity = " + product.getQuantity() + " KG's");
Picasso.get().load(product.getImage()).into(productViewHolder.imageView);
}
@NonNull
@Override
public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_items_layout, parent, false);
ProductViewHolder holder = new ProductViewHolder(view);
return holder;
}
};
recyclerView.setAdapter(adapter);
adapter.startListening();
}
}
上面页面的XML文件的代码为:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
tools:context=".FindFoodDonations">
<com.google.安卓.material.bottomnavigation.BottomNavigationView
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:id="@+id/bottom_organization_navigation"
app:itemBackground="@color/design_default_color_on_primary"
app:itemTextColor="@drawable/selector"
app:itemIconTint="@drawable/selector"
app:menu="@menu/organizationbottom_navigation"
安卓:layout_alignParentBottom="true"/>
还有一个链接的页面(我不知道是否有必要,但我会把它放在这里)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".HomeActivity">
<安卓.support.v7.widget.RecyclerView
安卓:id="@+id/recycler_menu"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:scrollbars="vertical"
>
</安卓.support.v7.widget.RecyclerView>
</RelativeLayout>
我以前没有遇到过这样的错误,所以我不知道该怎么办
# 1 楼答案
试试这个
而不是
# 2 楼答案
剪切
recyclerView.setHasFixedSize(true)
并将其粘贴在此代码recyclerView.setLayoutManager(layoutManager)
下方。希望它能起作用