Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference

huangapple 未分类评论67阅读模式
标题翻译

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:

&#39;Attempt to invoke virtual method &#39;void androidx.recyclerview.widget.RecyclerView.setHasFixedSize(boolean)&#39; on a null object reference&#39; and also said that its Caused by: java.lang.NullPointerException: Attempt to invoke virtual method &#39;void androidx.recyclerview.widget.RecyclerView.setHasFixedSize(boolean)&#39; 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(&quot;Products&quot;);

        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&lt;Product&gt; options =
                new FirebaseRecyclerOptions.Builder&lt;Product&gt;()
                        .setQuery(ProductsRef, Product.class)
                        .build();


        FirebaseRecyclerAdapter&lt;Product, ProductViewHolder&gt; adapter =
                new FirebaseRecyclerAdapter&lt;Product, ProductViewHolder&gt;(options) {
                    @Override
                    protected void onBindViewHolder(@NonNull ProductViewHolder productViewHolder, int i, @NonNull Product product) {
                        productViewHolder.txtProductName.setText(product.getPname());
                        productViewHolder.txtProductPrice.setText(&quot;Quantity = &quot; + product.getQuantity() + &quot; KG&#39;s&quot;);
                        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:

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;RelativeLayout
    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    tools:context=&quot;.FindFoodDonations&quot;&gt;
    &lt;com.google.android.material.bottomnavigation.BottomNavigationView
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:id=&quot;@+id/bottom_organization_navigation&quot;
        app:itemBackground=&quot;@color/design_default_color_on_primary&quot;
        app:itemTextColor=&quot;@drawable/selector&quot;
        app:itemIconTint=&quot;@drawable/selector&quot;
        app:menu=&quot;@menu/organizationbottom_navigation&quot;
        android:layout_alignParentBottom=&quot;true&quot;/&gt;

</RelativeLayout>

And a page that is linked (I dont know if its neccesary, but I'll just put it here)

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;RelativeLayout
    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    app:layout_behavior=&quot;@string/appbar_scrolling_view_behavior&quot;
    tools:context=&quot;.HomeActivity&quot;&gt;

    &lt;android.support.v7.widget.RecyclerView
        android:id=&quot;@+id/recycler_menu&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;
        android:scrollbars=&quot;vertical&quot;
        &gt;
    &lt;/android.support.v7.widget.RecyclerView&gt;

&lt;/RelativeLayout&gt;

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

&lt;androidx.recyclerview.widget.RecyclerView
            android:id=&quot;@+id/recycler_menu&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;match_parent&quot;
            android:scrollbars=&quot;vertical&quot;/&gt;

Instead of

&lt;android.support.v7.widget.RecyclerView
        android:id=&quot;@+id/recycler_menu&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;
        android:scrollbars=&quot;vertical&quot;
        &gt;
    &lt;/android.support.v7.widget.RecyclerView&gt;

答案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 ?

huangapple
  • 本文由 发表于 2020年3月16日 17:33:09
  • 转载请务必保留本文链接:https://java.coder-hub.com/60703409.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定