【问题标题】:Null Object Reference with Custom Array List. Where to instantiate?带有自定义数组列表的空对象引用。在哪里实例化?
【发布时间】:2020-11-09 03:10:03
【问题描述】:

试图完成我的估算项目真的碰壁了。想使用 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();
            }
        });

这是完整的活动

        
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();
    }

}


【问题讨论】:

  • 显示 logcat 和错误行。
  • 我回到电脑前就可以了。错误是estimates.add(newEstimate) 行中的空对象
  • 你确定estimates = intent.getParcelableArrayListExtra("estimates"); 工作正常吗?
  • 活动是如何开始的?意图可能没有预期的有效负载,最终将estimates(和其他字段)变为null

标签: java android instantiation


【解决方案1】:

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

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

intent.getParcelableArrayListExtra()之后也做空检查。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-02
    • 2015-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多