Olá, estou com dificuldades de carregar dados de uma intent para outra, será que poderiam me ajudar?
Tenho este botão que quero que carregue os dados:
private void botaoNovo() {
novaInspecao = findViewById(R.id.nova_insp);
novaInspecao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent vaiParaInspecao = new Intent(ListaInspecoesActivity.this, CadDataKmActivity.class);
vaiParaInspecao.putExtra("carro", carro);
startActivity(vaiParaInspecao);
}
});
}
E tenho este codigo na activity para receber:
private void carregaDadosCarro() {
Intent intent = getIntent();
Carro carro = (Carro) intent.getSerializableExtra("carro");
carronome = findViewById(R.id.carronome);
carronome.setText(carro.getNome());
idCarro = findViewById(R.id.carroid);
idCarro.setText(carro.getId().toString());
}
Porém, ele dá erro dizendo que o objeto está nulo...
E/AndroidRuntime: FATAL EXCEPTION: main
Process: br.com.hlnengenharia.app, PID: 29418
java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.hlnengenharia.app/br.com.hlnengenharia.app.cadastro.CadDataKmActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Long br.com.hlnengenharia.app.model.Carro.getId()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2572)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2654)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5728)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Long br.com.hlnengenharia.app.model.Carro.getId()' on a null object reference
at br.com.hlnengenharia.app.cadastro.CadDataKmActivity.carregaDadosCarro(CadDataKmActivity.java:55)
at br.com.hlnengenharia.app.cadastro.CadDataKmActivity.onCreate(CadDataKmActivity.java:41)
at android.app.Activity.performCreate(Activity.java:6301)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2519)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2654)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5728)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)