处理多个parcelable arraylists并获取' boolean java.util.ArrayList.add(java.lang.Object)'

时间:2017-10-04 01:20:04

标签: java android arraylist parcelable

所以基本上我有两个我目前正在研究的活动:其中一个有一个图像按钮和一个带有值的表。如果我按下按钮,它将把我带到另一个表格,用户必须填写以插入新的修复条目。想法是显示添加的每个条目。

以下是该活动的2个屏幕截图: CarRepair Activity NewRepair Activity

我正在使用可以与意图一起使用的parcelable arraylist,在其他活动上阅读它并来回传递它但我遇到了问题。每次我尝试输入CarRepair活动时,都会出现错误:

    "Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference
                at g3w.gescarcopytest.CarRepair.onCreate(CarRepair.java:74)

关于如何使这项工作的任何想法?

CarRepair.java

public class CarRepair extends AppCompatActivity 
{
TableLayout tl;
TableRow tr;
TableRow tr_head;

ArrayList<Repair> repairs = null;
ArrayList<CarDataset> carros;
Bundle bdl;
ImageButton newRepair;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_carrepair);

    bdl = getIntent().getExtras();

    if (bdl != null)
    {
        Log.e("teste1", "TESTE DEU");
        repairs = new ArrayList<>();
        carros = new ArrayList<>();
        carros = bdl.getParcelableArrayList("cars");
        repairs = bdl.getParcelableArrayList("repairs");
        //Log.e("teste3", "Matricula:" + repairs.get(0).licensePlate);
        Log.e("teste4", "Carro presente:" + carros.get(0).brand);
    } else
    {
        Toast.makeText(CarRepair.this, "O Bundle está vazio", Toast.LENGTH_LONG).show();
        Log.e("teste", "TESTE NAO DEU");
    }

    // THIS IS THE LINE WHICH THE ERROR IS COMING FROM!
    repairs.add(new Repair("82-AB-42", "08/03/1993", "Pneus", "Curvão", "1 Pneu", 50.0)); 

    // Log.e("teste4", "Veículo" + repairs.get(0).licensePlate);

    tl = (TableLayout) findViewById(R.id.tl_carrepair_2ndrow);
    createRepairTable(repairs);

    newRepair = (ImageButton)findViewById(R.id.imgbtn_carrepair_insert);

    newRepair.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Intent insertRepair = new Intent();
            insertRepair.putParcelableArrayListExtra("cars", carros);
            insertRepair.putParcelableArrayListExtra("repairs", repairs);
            insertRepair.setClass(CarRepair.this, NewRepair.class);
            startActivity(insertRepair);
        }
    });
}

public void createRepairTable(ArrayList<Repair> repairs)
{
    tr_head = new TableRow(this);
    tr_head.setBackgroundColor(Color.GRAY);
    tr_head.setId(View.generateViewId());
    tr_head.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));

    TextView lable_licensePlate = new TextView(this);
    lable_licensePlate.setId(View.generateViewId());
    lable_licensePlate.setText("Matricula");
    lable_licensePlate.setTextColor(Color.BLACK);
    lable_licensePlate.setPadding(5, 5, 5, 5);
    tr_head.addView(lable_licensePlate);

    TextView lable_date = new TextView(this);
    lable_date.setId(View.generateViewId());
    lable_date.setText("Data");
    lable_date.setTextColor(Color.BLACK);
    lable_date.setPadding(5,5,5,5);
    tr_head.addView(lable_date);

    TextView lable_typeOfCost = new TextView(this);
    lable_typeOfCost.setId(View.generateViewId());
    lable_typeOfCost.setText("Tipo de Custo");
    lable_typeOfCost.setTextColor(Color.BLACK);
    lable_typeOfCost.setPadding(5,5,5,5);
    tr_head.addView(lable_typeOfCost);

    TextView lable_supplier = new TextView(this);
    lable_supplier.setId(View.generateViewId());
    lable_supplier.setText("Fornecedor");
    lable_supplier.setTextColor(Color.BLACK);
    lable_supplier.setPadding(5,5,5,5);
    tr_head.addView(lable_supplier);

    TextView lable_description = new TextView(this);
    lable_description.setId(View.generateViewId());
    lable_description.setText("Descrição");
    lable_description.setTextColor(Color.BLACK);
    lable_description.setPadding(5,5,5,5);
    tr_head.addView(lable_description);

    TextView lable_cost = new TextView(this);
    lable_cost.setId(View.generateViewId());
    lable_cost.setText("Custo");
    lable_cost.setTextColor(Color.BLACK);
    lable_cost.setPadding(5, 5, 5, 5);
    tr_head.addView(lable_cost);

    tl.addView(tr_head, new TableLayout.LayoutParams(TableRow.LayoutParams.FILL_PARENT, android.app.ActionBar.LayoutParams.WRAP_CONTENT));


    for (int i = 0; i < repairs.size(); i++)
    {
        tr = new TableRow(this);
        tr.setId(View.generateViewId());
        tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));

        TextView lable_licensePlateCar = new TextView(this);
        lable_licensePlateCar.setId(View.generateViewId());
        lable_licensePlateCar.setText(repairs.get(i).licensePlate);
        lable_licensePlateCar.setTextColor(Color.BLACK);
        lable_licensePlateCar.setPadding(2, 0, 5, 0);
        tr.addView(lable_licensePlateCar);

        TextView lable_dateCar = new TextView(this);
        lable_dateCar.setId(View.generateViewId());
        lable_dateCar.setText(repairs.get(i).date);
        lable_dateCar.setTextColor(Color.BLACK);
        lable_dateCar.setPadding(2, 0, 5, 0);
        tr.addView(lable_dateCar);

        TextView lable_typeOfCostCar = new TextView(this);
        lable_typeOfCostCar.setId(View.generateViewId());
        lable_typeOfCostCar.setText(repairs.get(i).typeOfCost);
        lable_typeOfCostCar.setTextColor(Color.BLACK);
        lable_typeOfCostCar.setPadding(2, 0, 5, 0);
        tr.addView(lable_typeOfCostCar);

        TextView lable_supplierCar = new TextView(this);
        lable_supplierCar.setId(View.generateViewId());
        lable_supplierCar.setText(repairs.get(i).supplier);
        lable_supplierCar.setTextColor(Color.BLACK);
        lable_supplierCar.setPadding(2, 0, 5, 0);
        tr.addView(lable_supplierCar);

        TextView lable_descriptionCar = new TextView(this);
        lable_descriptionCar.setId(View.generateViewId());
        lable_descriptionCar.setText(repairs.get(i).description);
        lable_descriptionCar.setTextColor(Color.BLACK);
        lable_descriptionCar.setPadding(2, 0, 5, 0);
        tr.addView(lable_descriptionCar);

        TextView lable_costCar = new TextView(this);
        lable_costCar.setId(View.generateViewId());
        lable_costCar.setText(repairs.get(i).cost.toString());
        lable_costCar.setTextColor(Color.BLACK);
        lable_costCar.setPadding(2, 0, 5, 0);
        tr.addView(lable_costCar);

        tl.addView(tr, new TableLayout.LayoutParams(TableRow.LayoutParams.FILL_PARENT, android.app.ActionBar.LayoutParams.WRAP_CONTENT));
    }

}

}

NewRepair.java

public class NewRepair extends AppCompatActivity {

ArrayList<CarDataset> carros = null;
ArrayList<String> carPlates = null;
ArrayList<Repair> repairs = null;
Bundle bdl;

private Spinner spViaturas;
private Spinner spTipoDespesa;
private Spinner spFonecedor;
private String date;
private String description;
private String licensePlate;
private String typeOfCost;
private String supplier;
private String tempCost;
private Double cost;

private TextView mDisplayDate;
private DatePickerDialog.OnDateSetListener mDateSetListener;
private EditText etDescription;
private EditText etCost;
private Button insertNewRepair;
private Button cancelNewRepair;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new_repair);

    bdl = getIntent().getExtras();

    if (bdl != null) {
        Log.e("teste1", "TESTE DEU");
        repairs = new ArrayList<>();
        carros = new ArrayList<>();
        carros = bdl.getParcelableArrayList("cars");
        repairs = bdl.getParcelableArrayList("repairs");
        Log.e("teste3", "Matricula:" + repairs.get(0).licensePlate);
        Log.e("teste4", "Carro presente:" + carros.get(0).brand);
    } else {
        Toast.makeText(NewRepair.this, "O Bundle está vazio", Toast.LENGTH_LONG).show();
        Log.e("teste", "TESTE NAO DEU");
    }
    carPlates = new ArrayList<>();
    getCarsPlates(carros);

    insertNewRepair = (Button) findViewById(R.id.btn_newrepair_insert);
    cancelNewRepair = (Button) findViewById(R.id.btn_newrepair_cancel);
    mDisplayDate = (TextView) findViewById(R.id.tv_newrepair_data);
    etDescription = (EditText) findViewById(R.id.et_newrepair_descricao);
    etCost = (EditText) findViewById(R.id.et_newrepair_custo);


    // Adaptador para o Spinner de carros
    ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, carPlates);
    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // Adaptador para o Spinner de Tipo de Despesa
    ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this, R.array.tipoDespesa, android.R.layout.simple_spinner_dropdown_item);
    adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // Adaptador para o Spinner de Fornecedor
    ArrayAdapter<CharSequence> adapter3 = ArrayAdapter.createFromResource(this, R.array.fornecedor, android.R.layout.simple_spinner_dropdown_item);
    adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    // Definir localização dos spinners
    spViaturas = (Spinner) findViewById(R.id.sp_newrepair_viaturas);
    spTipoDespesa = (Spinner) findViewById(R.id.sp_newrepair_tipoDespesa);
    spFonecedor = (Spinner) findViewById(R.id.sp_newrepair_fornecedor);

    // Definir os adaptadores respetivos de cada spinner
    spViaturas.setAdapter(adapter1);
    spTipoDespesa.setAdapter(adapter2);
    spFonecedor.setAdapter(adapter3);


    // O que fazer quando o item for selecionado ou não
    spViaturas.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

    // Quando é clicado para inserir uma data
    mDisplayDate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Calendar cal = Calendar.getInstance();
            int year = cal.get(Calendar.YEAR);
            int month = cal.get(Calendar.MONTH);
            int day = cal.get(Calendar.DAY_OF_MONTH);

            DatePickerDialog dialog = new DatePickerDialog(NewRepair.this, android.R.style.Theme_Holo_Light_Dialog_MinWidth, mDateSetListener, year, month, day);
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            dialog.show();
        }
    });

    mDateSetListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker datePicker, int year, int month, int day) {
            date = day + "/" + month + "/" + year;
            mDisplayDate.setText(date);
        }
    };

    // Valor inserido no editText de custo
    tempCost = etCost.getText().toString();
    if (!tempCost.isEmpty())
    {
        try
        {
            cost = Double.parseDouble(tempCost);

        }
        catch (Exception e1)
        {
            e1.printStackTrace();
            Toast.makeText(this, "Valor inválido, tente de novo", Toast.LENGTH_SHORT).show();
        }
    }

    // Quando é clicado no botão para inserir uma reparaçao nova
    insertNewRepair.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            description = etDescription.getText().toString();
            licensePlate = spViaturas.getSelectedItem().toString();
            typeOfCost = spTipoDespesa.getSelectedItem().toString();
            supplier = spFonecedor.getSelectedItem().toString();

            repairs.add(new Repair(licensePlate, date, typeOfCost, supplier, description, cost));
            Intent insertNewRepair = new Intent();
            insertNewRepair.putParcelableArrayListExtra("cars", carros);
            insertNewRepair.putParcelableArrayListExtra("repairs", repairs);
            insertNewRepair.setClass(NewRepair.this, CarRepair.class);
            startActivity(insertNewRepair);

        }
    });
    // Quando é clicado no botao para cancelar uma reparacao nova
    cancelNewRepair.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Intent insertNewRepair = new Intent();
            insertNewRepair.putParcelableArrayListExtra("cars", carros);
            insertNewRepair.putParcelableArrayListExtra("repairs", repairs);
            insertNewRepair.setClass(NewRepair.this, CarRepair.class);
            startActivity(insertNewRepair);
        }
    });

}

public void getCarsPlates ( ArrayList<CarDataset> cars)
{
    for(int i = 0; i < cars.size(); i++)
    {
        carPlates.add(carros.get(i).licencePlate);
    }
}

}

**问题已解决**

在阅读完答案并经过一些研究后,我修改了一些部分,现在它适用于我。

ArrayList<Repair> repairs = null;
ArrayList<CarDataset> carros = null;  

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_carrepair); 

    carros = getIntent().getParcelableArrayListExtra("cars");
    if(carros == null)
    {
        carros = new ArrayList<>();
    }
    repairs = getIntent().getParcelableArrayListExtra("repairs");
    if(repairs == null)
    {
        repairs = new ArrayList<>();
    }

    ...
}

2 个答案:

答案 0 :(得分:1)

您对捆绑包进行空检查。

    Option Explicit
Sub HostNameFinder()
Dim srchLen, hName, nxtRw As String
Dim g As Range
'Clear Sheet 2 and Copy Column Headings
 Sheets(2).Cells.ClearContents
 Sheets(1).Rows(1).Copy Destination:=Sheets(2).Rows(1)
'Determine length of Search Column from Sheet3
   srchLen = Sheets(3).Range("A" & Rows.Count).End(xlUp).Row
'Loop through list in Sheet3, Column A. As each value is 'found in Sheet1, Column A, copy it top the next row in Sheet2
  With Sheets(1).Columns("A")
    For hName = 2 To srchLen
      Set g = .Find(Sheets(3).Range("A" & hName), lookat:=xlWhole)
        If Not g Is Nothing Then
          nxtRw = Sheets(2).Range("A" & Rows.Count).End(xlUp).Row + 1
          g.EntireRow.Copy Destination:=Sheets(2).Range("A" & nxtRw)
        End If
    Next
  End With
End Sub

你基本上不会新建你的ArrayList,因此有一个空数组。除非您已经执行savedStateInstance来存储要从另一个活动中检索或传递的值,否则该包始终为null。

您可能没有传递任何捆绑附加内容,因此具有空捆绑,因此您的ArrayLists永远不会被实例化。

答案 1 :(得分:1)

从Intent接收数据时,应检查以下空值:

carros = bdl.getParcelableArrayList ("cars");
if(carros == null){
    carros = new ArrayList<>();
}

在所有4个地方进行类似的检查。