尝试对空对象引用调用虚拟方法“void androidx.recyclerview.widget.RecyclerView.setAdapter

rlcwz9us  于 2023-06-20  发布在  Android
关注(0)|答案(1)|浏览(102)

原因:java.lang.NullPointerException:尝试在com.pradeepuct.biboxlabstask.PartSelection.onCreate(PartSelection.java:49)的空对象引用上调用虚拟方法“void androidx.recyclerview.widget.RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView$Adapter)”

package com.pradeepuct.biboxlabstask;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class PartSelection extends AppCompatActivity implements PartShowListener {

    Button buttonNext;

    \\On create method
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_part_selection);

        RecyclerView partsRecyclerView = findViewById(R.id.recyclerView);

        buttonNext = findViewById(R.id.buttonNext);

        List<Parts> parts = new ArrayList<>();

        Parts handle = new Parts();
        handle.image = R.drawable.handle;
        handle.partName = "Handle Bar";
        parts.add(handle);

        Parts frame = new Parts();
        frame.image = R.drawable.frame;
        handle.partName = "Cycle Frame";
        parts.add(frame);

        Parts wheel = new Parts();
        wheel.image = R.drawable.wheel;
        wheel.partName = "Cycle Wheel";
        parts.add(wheel);

        final PartsAdapter partsAdapter = new PartsAdapter(parts, this);
        partsRecyclerView.setAdapter(partsAdapter);

        buttonNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<Parts> selectedParts = partsAdapter.getSelectedParts();

                StringBuilder partsNames = new StringBuilder();
                for (int i = 0; i < selectedParts.size(); i++) {
                    if (i == 0) {
                        partsNames.append(selectedParts.get(i).partName);
                    } else {
                        partsNames.append("\n").append(selectedParts.get(i).partName);
                    }
                }
                Toast.makeText(PartSelection.this, partsNames.toString(),Toast.LENGTH_SHORT).show();
            }
        });
    }

    \\Part show action
    @Override
    public void onPartShowAction(boolean isSelected) {

        if (isSelected) {
            buttonNext.setVisibility(View.VISIBLE);
        } else {
            buttonNext.setVisibility(View.GONE);
        }
    }
}

它正在为回收器视图抛出空指针异常

irlmq6kh

irlmq6kh1#

尝试添加.setLayoutManager()检查下面的代码

RecyclerView partsRecyclerView = findViewById(R.id.recyclerView);

    buttonNext = findViewById(R.id.buttonNext);

    List<Parts> parts = new ArrayList<>();

    Parts handle = new Parts();
    handle.image = R.drawable.handle;
    handle.partName = "Handle Bar";
    parts.add(handle);

    Parts frame = new Parts();
    frame.image = R.drawable.frame;
    handle.partName = "Cycle Frame";
    parts.add(frame);

    Parts wheel = new Parts();
    wheel.image = R.drawable.wheel;
    wheel.partName = "Cycle Wheel";
    parts.add(wheel);

    partsRecyclerView.setLayoutManager(new LinearLayoutManager(this)); //add this line
    final PartsAdapter partsAdapter = new PartsAdapter(parts, this);
    partsRecyclerView.setAdapter(partsAdapter);

相关问题