标题翻译
Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
问题
基本上,当我运行我的应用程序时,应用程序没有给我任何错误,但是,当我尝试在我的应用程序中打开一个名为“查找食物捐赠”的页面时,它在日志中给我返回了这个错误代码:
'尝试在空对象引用上调用虚拟方法'void androidx.recyclerview.widget.RecyclerView.setHasFixedSize(boolean)',并且还说是由于:java.lang.NullPointerException:在空对象引用上调用虚拟方法'void androidx.recyclerview.widget.RecyclerView.setHasFixedSize(boolean)':
在com.example.myddproject.FindFoodDonations.onCreate(FindFoodDonations.java:66)处。
以下是应用程序代码,在Java文件中发生错误的地方(行已加粗):
public class FindFoodDonations extends AppCompatActivity{
// ...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find_food_donations);
// ...
recyclerView = findViewById(R.id.recycler_menu);
**recyclerView.setHasFixedSize(true);**
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
}
// ...
}
页面上的XML文件代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FindFoodDonations">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android: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"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
此外,还有一个链接到的页面(我不知道是否必要,但我会把它放在这里):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".HomeActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
我以前没有遇到过这种错误,所以我不知道该怎么办。
英文翻译
so basically when I run my app, the app does not give me any errors however, when I tried open a page called 'Find Food Donations' in my app, it gave me this error code in the log:
'Attempt to invoke virtual method 'void androidx.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 androidx.recyclerview.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference:
at com.example.myddproject.FindFoodDonations.onCreate(FindFoodDonations.java:66).
Below is the code for the app where the error is occurring in the Java File (the line is bolded)
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();
}
}
The code for the XML File for the page above is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FindFoodDonations">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android: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"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
And a page that is linked (I dont know if its neccesary, but I'll just put it here)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".HomeActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
>
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
I have not encountered such a error before so I don't know what to do.
答案1
得分: 0
试试这个:
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"/>
而不是:
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
英文翻译
Try this
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"/>
Instead of
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
>
</android.support.v7.widget.RecyclerView>
答案2
得分: 0
将recyclerView.setHasFixedSize(true)
这一行剪切并粘贴到此代码recyclerView.setLayoutManager(layoutManager)
的下方。希望这样能够正常工作?
英文翻译
Cut the recyclerView.setHasFixedSize(true)
and paste it below this code recyclerView.setLayoutManager(layoutManager)
. Hope it will work ?
专注分享java语言的经验与见解,让所有开发者获益!
评论