带有自定义数组列表的空对象引用。在哪里实例化?

Null Object Reference with Custom Array List. Where to instantiate?

在完成我的估算项目时真的碰壁了。想使用 gson 将自定义 ArrayList 保存到 sharedPreferences。我需要将估计值添加到估计值(自定义 ArrayList)。我已经阅读了十几个其他类似的帖子,甚至以前也攻克过一个类似的问题。我一直在疯狂地尝试弄清楚我没有正确实例化什么以及我应该在哪里这样做。我觉得我到处都试过了,但我显然遗漏了一些东西。

下面代码中的 .add() 有问题。

          saveButton = findViewById(R.id.save);

        saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                estimates.add(newEstimate);
                saveData();
            }
        });

这是 Activity 的完整内容

        
public class EstimateActivity extends AppCompatActivity {
    private RecyclerView eRecyclerview;
    private CartAdapter eAdapter;
    private RecyclerView.LayoutManager eLayoutManager;
    private ArrayList<Inventory> eCartList;
    private TextView clientName, clientEmail, workOrder;
    private Button saveButton;
    private ArrayList<Estimate> estimates;
    private Estimate newEstimate;
    private String name, code, email;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_estimate);
        estimates = new ArrayList<>();
        newEstimate = new Estimate();

        //get intents
        Intent intent = getIntent();
        newEstimate = intent.getParcelableExtra("newEstimate");
        estimates = intent.getParcelableArrayListExtra("estimates");
        eCartList = intent.getParcelableArrayListExtra("cartList");

        //set up textViews
        workOrder= findViewById(R.id.cart_work_order);
        clientName = findViewById(R.id.client_name_cart);
        clientEmail = findViewById(R.id.cart_email);

         name = newEstimate.getClientName();
        code = newEstimate.getWorkOrder();
        email = newEstimate.getClientEmail();

        workOrder.setText(code);
        clientName.setText(name);
        clientEmail.setText(email);

        //connect recyclerView
        eRecyclerview = findViewById(R.id.recyclerview_cart);
        eLayoutManager = new LinearLayoutManager(this);
        eAdapter = new CartAdapter(eCartList);
        eRecyclerview.setLayoutManager(eLayoutManager);
        eRecyclerview.setAdapter(eAdapter);
        newEstimate.setCart(eCartList);

        //Set up save button
        saveButton = findViewById(R.id.save);

        saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                estimates.add(newEstimate);
                saveData();
            }
        });


    }


    private void saveData(){
        SharedPreferences sharedPreferences = getSharedPreferences("share preferences", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        Gson gson = new Gson();
        String json = gson.toJson(estimates);
        editor.putString("task list", json);
        editor.apply();
    }

}


估计,库存Class必须实现Parcelable接口。

根据 Android API Reference,您必须 describeContents()writeToParcel(Parcel dest, int flags) 正确。

并在 intent.getParcelableArrayListExtra() 之后进行空值检查。