0
I have this mistake and I can’t figure out how to fix it.
Attempt to invoke virtual method 'android.content.res.Resources android.view.View.getResources()' on a null Object Reference
This is the code of actCadCliente
:
package com.betelhosting.dev.carteiradecliente;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Patterns;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.betelhosting.dev.carteiradecliente.database.DadosOpenHelper;
import com.betelhosting.dev.carteiradecliente.dominio.entidades.Cliente;
import com.betelhosting.dev.carteiradecliente.dominio.repositorio.ClienteRepositorio;
public class ActCadCliente extends AppCompatActivity {
private EditText edtNome;
private EditText edtEndereco;
private EditText edtEmail;
private EditText edtTelefone;
private ConstraintLayout layoutContentActCadCliente;
private ClienteRepositorio clienteRepositorio;
private SQLiteDatabase conexao;
private DadosOpenHelper dadosOpenHelper;
private Cliente cliente;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_cad_cliente);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
edtNome = (EditText) findViewById(R.id.edtNome);
edtEndereco = (EditText) findViewById(R.id.edtEndereco);
edtEmail = (EditText) findViewById(R.id.edtEmail);
edtTelefone = (EditText) findViewById(R.id.edtTelefone);
layoutContentActCadCliente = (ConstraintLayout) findViewById(R.id.layoutContentActCadCliente);
criarConexao();
}
private void criarConexao() {
try {
dadosOpenHelper = new DadosOpenHelper(this);
conexao = dadosOpenHelper.getWritableDatabase();
Snackbar.make(layoutContentActCadCliente, R.string.message_conexao_criada_com_sucesso, Snackbar.LENGTH_LONG)
.setAction(R.string.lbl_ok, null).show();
clienteRepositorio = new ClienteRepositorio(conexao);
} catch (Exception ex) {
android.support.v7.app.AlertDialog.Builder dlg = new android.support.v7.app.AlertDialog.Builder(this);
dlg.setTitle(R.string.title_erro);
dlg.setMessage(ex.getMessage());
dlg.setNeutralButton(R.string.action_ok, null);
dlg.show();
}
}
private void confirmar() {
cliente = new Cliente();
if (validaCampos() == false) {
try {
clienteRepositorio.inserir(cliente);
finish();
} catch (Exception ex) {
android.support.v7.app.AlertDialog.Builder dlg = new android.support.v7.app.AlertDialog.Builder(this);
dlg.setTitle(R.string.title_erro);
dlg.setMessage(ex.getMessage());
dlg.setNeutralButton(R.string.action_ok, null);
dlg.show();
}
}
}
@SuppressLint("ResourceType")
private boolean validaCampos() {
boolean res = false;
String nome = edtNome.getText().toString();
String endereco = edtEndereco.getText().toString();
String email = edtEmail.getText().toString();
String telefone = edtTelefone.getText().toString();
if (res = isCampoVazio(nome)) {
edtNome.requestFocus();
} else
if (res = isCampoVazio(endereco)) {
edtEndereco.requestFocus();
} else
if (res = !isEmailValido(email)) {
edtEmail.requestFocus();
} else
if (res = isCampoVazio(telefone)) {
edtTelefone.requestFocus();
}
if (res) {
AlertDialog.Builder dlg = new AlertDialog.Builder(this);
dlg.setTitle("Aviso");
dlg.setMessage(R.string.message_campos_invalidos_brancos);
dlg.setNeutralButton(R.string.action_ok, null);
dlg.show();
}
return res;
}
private boolean isCampoVazio(String valor) {
boolean resultado = (TextUtils.isEmpty(valor) || valor.trim().isEmpty());
return resultado;
}
private boolean isEmailValido(String email) {
boolean resultado = (!isCampoVazio(email) && Patterns.EMAIL_ADDRESS.matcher(email).matches());
return resultado;
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_act_cad_cliente, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_ok:
validaCampos();
//Toast.makeText(this, "Botão OK Selecionado", Toast.LENGTH_SHORT).show();
break;
case R.id.action_cancelar:
//Toast.makeText(this, "Botão OK Selecionado", Toast.LENGTH_SHORT).show();
finish();
break;
}
return super.onOptionsItemSelected(item);
}
}
actMain
:
package com.betelhosting.dev.carteiradecliente;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import com.betelhosting.dev.carteiradecliente.database.DadosOpenHelper;
import java.sql.SQLException;
public class ActMain extends AppCompatActivity {
private RecyclerView lstDados;
private FloatingActionButton fab;
private ConstraintLayout layoutContentMain;
private SQLiteDatabase conexao;
private DadosOpenHelper dadosOpenHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
fab = (FloatingActionButton) findViewById(R.id.fab);
lstDados = (RecyclerView) findViewById(R.id.lstDados);
layoutContentMain = (ConstraintLayout) findViewById(R.id.layoutContentMain);
criarConexao();
}
private void criarConexao() {
try {
dadosOpenHelper = new DadosOpenHelper(this);
conexao = dadosOpenHelper.getWritableDatabase();
Snackbar.make(layoutContentMain, R.string.message_conexao_criada_com_sucesso, Snackbar.LENGTH_LONG)
.setAction(R.string.action_ok, null).show();
} catch (Exception ex) {
AlertDialog.Builder dlg = new AlertDialog.Builder(this);
dlg.setTitle(R.string.title_erro);
dlg.setMessage(ex.getMessage());
dlg.setNeutralButton(R.string.action_ok, null);
dlg.show();
}
}
public void cadastrar(View view) {
Intent it = new Intent(ActMain.this, ActCadCliente.class);
startActivity(it);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_act_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
ClienteRepositorio
:
package com.betelhosting.dev.carteiradecliente.dominio.repositorio;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.betelhosting.dev.carteiradecliente.dominio.entidades.Cliente;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Felipe Tomaz Rebeca, dev.betelhosting.com/i
*/
public class ClienteRepositorio {
private SQLiteDatabase conexao;
public ClienteRepositorio(SQLiteDatabase conexao) {
this.conexao = conexao;
}
public void inserir(Cliente cliente) {
ContentValues contentValues = new ContentValues();
contentValues.put("NOME", cliente.nome);
contentValues.put("ENDERECO", cliente.endereco);
contentValues.put("EMAIL", cliente.email);
contentValues.put("TELEFONE", cliente.telefone);
conexao.insertOrThrow("CLIENTE", null, contentValues);
}
public void excluir(int codigo) {
String[] parametros = new String[1];
parametros[0] = String.valueOf(codigo);
conexao.delete("CLIENTE", "CODIGO = ?", parametros);
}
public void alterar(Cliente cliente) {
ContentValues contentValues = new ContentValues();
contentValues.put("NOME", cliente.nome);
contentValues.put("ENDERECO", cliente.endereco);
contentValues.put("EMAIL", cliente.email);
contentValues.put("TELEFONE", cliente.telefone);
String[] parametros = new String[1];
parametros[0] = String.valueOf(cliente.codigo);
conexao.update("CLIENTE", contentValues, "CODIGO = ?", parametros);
}
public List < Cliente > buscarTodos() {
List < Cliente > clientes = new ArrayList < Cliente > ();
StringBuilder sql = new StringBuilder();
sql.append(" SELECT CODIGO, NOME, ENDERECO, EMAIL, TELEFONE ");
sql.append(" FROM CLIENTE ");
Cursor resultado = conexao.rawQuery(sql.toString(), null);
if (resultado.getCount() > 0) {
resultado.moveToFirst();
do {
Cliente cli = new Cliente();
cli.codigo = resultado.getInt(resultado.getColumnIndexOrThrow("CODIGO"));
cli.nome = resultado.getString(resultado.getColumnIndexOrThrow("NOME"));
cli.endereco = resultado.getString(resultado.getColumnIndexOrThrow("ENDERECO"));
cli.email = resultado.getString(resultado.getColumnIndexOrThrow("EMAIL"));
cli.telefone = resultado.getString(resultado.getColumnIndexOrThrow("TELEFONE"));
clientes.add(cli);
} while (resultado.moveToNext());
}
return clientes;
}
public Cliente buscarCliente(int codigo) {
Cliente cliente = new Cliente();
StringBuilder sql = new StringBuilder();
sql.append(" SELECT CODIGO, NOME, ENDERECO, EMAIL, TELEFONE ");
sql.append(" FROM CLIENTE ");
sql.append(" WHERE CODIGO = ?");
String[] parametros = new String[1];
parametros[0] = String.valueOf(cliente.codigo);
Cursor resultado = conexao.rawQuery(sql.toString(), null);
if (resultado.getCount() > 0) {
resultado.moveToFirst();
cliente.codigo = resultado.getInt(resultado.getColumnIndexOrThrow("CODIGO"));
cliente.nome = resultado.getString(resultado.getColumnIndexOrThrow("NOME"));
cliente.endereco = resultado.getString(resultado.getColumnIndexOrThrow("ENDERECO"));
cliente.email = resultado.getString(resultado.getColumnIndexOrThrow("EMAIL"));
cliente.telefone = resultado.getString(resultado.getColumnIndexOrThrow("TELEFONE"));
return cliente;
}
return null;
}
}
from now on thank.
When your error is raised, you need to explain to us in which action of your application is given the error. In some cases, adding the code and showing the error doesn’t help much.
– RXSD
when I enter the registration view...well I still don’t know how to ask clear mt questions about android pq I am beginner in this language...
– FelipeBr193
is not clear enough - Clarify your specific issue or add other details to highlight exactly what you need. The way it’s written here, it’s hard to know exactly what you’re asking. See the How to Ask page for help clarifying this question.
– Bruno Romualdo
I need to fix it because Saturday I’ll deliver it...
– FelipeBr193