0
I’m trying to run this Activity as main, but the application even runs on mobile.
The error points to the following lines of code:
FormularioHelper helper = new FormularioHelper(activity);
this.campoNome = activity.findViewById(R.id.nome);
Listaalunosactivity.java
package com.alura.magnero2018.agendaalura;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import java.io.Serializable;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;
import alura.modelo.Aluno;
import br.com.alura.dao.AlunoDAO;
public class ListaAlunosActivity extends AppCompatActivity {
private ListView listaAlunos;
FormularioActivity activity;
private FormularioHelper helper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista_alunos);
Button btnNovoAluno = findViewById(R.id.btnNovoAluno);
listaAlunos = findViewById(R.id.lista_alunos);
**FormularioHelper helper = new FormularioHelper(activity);**
Intent intent = getIntent();
Aluno aluno = ((Aluno) intent.getSerializableExtra("aluno"));
if (aluno.getNome() !=null) {
helper.preencheFormulario(aluno);
}
listaAlunos.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> lista, View item, int position, long id) {
Aluno aluno = (Aluno) listaAlunos.getItemAtPosition(position);
Intent intentVaiProFormulario = new Intent(ListaAlunosActivity.this, FormularioActivity.class);
intentVaiProFormulario.putExtra("aluno", (Serializable) aluno);
startActivity(intentVaiProFormulario);
}
});
carregaLista();
registerForContextMenu(listaAlunos);
btnNovoAluno.setOnClickListener((v) -> {
Intent abrirFormulario = new Intent(ListaAlunosActivity.this, FormularioActivity.class);
startActivity(abrirFormulario);
});
}
private void carregaLista() {
AlunoDAO dao = new AlunoDAO(this);
//cria uma lista com os dados buscados do banco
List<Aluno> alunos = dao.buscaAlunos();
dao.close();
/* pega os dados do contexto do layour lista_alunos e transforma-os em ListView */
ListView listaAlunos = findViewById(R.id.lista_alunos);
/* transforma os dados do array alunos de string para view */
ArrayAdapter<Aluno> adapter = new ArrayAdapter<Aluno>(this, android.R.layout.simple_list_item_1, alunos);
listaAlunos.setAdapter(adapter);
}
protected void onResume()
{
super.onResume();
}
//quando o menu de contexto comecar a ser criado, serao inseridas funcionalidades no menu
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
{
MenuItem deletar = menu.add("Deletar");
deletar.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
Aluno aluno = (Aluno) listaAlunos.getItemAtPosition(info.position);
AlunoDAO dao = new AlunoDAO(ListaAlunosActivity.this);
dao.deleta(aluno);
dao.close();
Toast.makeText(ListaAlunosActivity.this, "Aluno " + aluno.getNome() + " deletado com sucesso!", Toast.LENGTH_SHORT).show();
carregaLista();
return false;
}
});
}
}
Android Studio returns me the following errors:
Error log
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.alura.magnero2018.agendaalura, PID: 12337
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.alura.magnero2018.agendaalura/com.alura.magnero2018.agendaalura.ListaAlunosActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View com.alura.magnero2018.agendaalura.FormularioActivity.findViewById(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3121)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3260)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1976)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6912)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:860)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View com.alura.magnero2018.agendaalura.FormularioActivity.findViewById(int)' on a null object reference
at com.alura.magnero2018.agendaalura.FormularioHelper.<init>(FormularioHelper.java:64)
at com.alura.magnero2018.agendaalura.ListaAlunosActivity.onCreate(ListaAlunosActivity.java:37)
at android.app.Activity.performCreate(Activity.java:7144)
at android.app.Activity.performCreate(Activity.java:7135)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1293)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3101)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3260)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1976)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6912)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:860)
Application terminated.
activity_list_students.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ListaAlunosActivity"
android:orientation="vertical">
<ListView
android:id="@+id/lista_alunos"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnNovoAluno"
android:layout_width="56dp"
android:layout_height="56dp"
android:text="+"
android:textColor="#ffffff"
android:textSize="35sp"
android:elevation="6dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="16dp"
android:layout_marginRight="16dp"
android:background="@drawable/fundo"
/>
</RelativeLayout>
Forumulariohelper.java
package com.alura.magnero2018.agendaalura;
import android.widget.EditText;
import android.widget.RatingBar;
import com.alura.magnero2018.agendaalura.FormularioActivity;
import com.alura.magnero2018.agendaalura.R;
import alura.modelo.Aluno;
public class FormularioHelper
{
private EditText campoNome;
private EditText campoEndereco;
private EditText campoSitesPessoais;
private EditText campoTelefone;
private RatingBar campoNotas;
private Aluno aluno;
public EditText getCampoNome() {
return campoNome;
}
public void setCampoNome(EditText campoNome) {
this.campoNome = campoNome;
}
public EditText getCampoEndereco() {
return campoEndereco;
}
public void setCampoEndereco(EditText campoEndereco) {
this.campoEndereco = campoEndereco;
}
public EditText getCampoSitesPessoais() {
return campoSitesPessoais;
}
public void setCampoSitesPessoais(EditText campoSitesPessoais) {
this.campoSitesPessoais = campoSitesPessoais;
}
public EditText getCampoTelefone() {
return campoTelefone;
}
public void setCampoTelefone(EditText campoTelefone) {
this.campoTelefone = campoTelefone;
}
public RatingBar getCampoNotas() {
return campoNotas;
}
public void setCampoNotas(RatingBar campoNotas) {
this.campoNotas = campoNotas;
}
public FormularioHelper(FormularioActivity activity)
{
**this.campoNome = activity.findViewById(R.id.nome);**
this.campoEndereco = activity.findViewById(R.id.endereco);
this.campoSitesPessoais = activity.findViewById(R.id.sitesPessoais);
this.campoTelefone = activity.findViewById(R.id.telefone);
this.campoNotas = (RatingBar) activity.findViewById(R.id.notas);
Aluno aluno = new Aluno();
}
public Aluno pegarAluno()
{
Aluno aluno = new Aluno();
aluno.setNome(String.valueOf(campoNome.getText()));
aluno.setEndereco(String.valueOf(campoEndereco.getText()));
aluno.setSite(String.valueOf(campoSitesPessoais.getText()));
aluno.setTelefone(String.valueOf(campoTelefone.getText()));
aluno.setNota(Double.valueOf(campoNotas.getProgress()));
return aluno;
}
public void preencheFormulario(Aluno aluno) {
campoNome.setText(aluno.getNome());
campoEndereco.setText(aluno.getEndereco());
campoTelefone.setText(aluno.getTelefone());
campoSitesPessoais.setText(aluno.getSite());
campoNotas.setProgress(aluno.getNota().intValue());
this.aluno = aluno;
}
}
I had the same perception, but I don’t know if it would lead to a mistake
– Vinicius Lino
@Viniciuslino I am searching here to see if I find any reference in relation to this. But from what I have read so far, it can lead to error yes. If you find something, I’ll update the post
– Leonardo Assunção
The problem was the name of the onItemClick method, the l was capitalized. I copied the code from the course I’m doing, and it took me two days to realize that. It is not the first problem I have with the codes of this course, and this is very annoying for those who are learning.
– Laura Regina
Yesterday after I solved these problems there were others, but I can’t edit the post yet because the R class is not being recognized by my Android Studio, and I’m trying to fix this to get to see again the mistakes of yesterday.
– Laura Regina
I edited the post, with the edited code and the new error log.
– Laura Regina
@Lauraregina post xml activity_list students please. You are probably calling some view that is not present in Layout
– Leonardo Assunção
I edited the post and put the xml.
– Laura Regina