0
I’m almost two weeks with this problem in my android code, I already searched in some forums and nothing, so I’m kind of desperate looking for help. I’m new dealing with android, if anyone can help me.
Follows the codes:
Novaquery.java:
public class NovaConsulta extends Activity {
private Date data;
private int ano, mes, dia;
private Button dataButton;
private EditText tipo_consultaET, dia_consultaET, localET,telefoneET,enderecoET,latitudeET,longitudeET,situacaoET,descricaoET;
private Long id;
private AvisaSusDAO dao;
private CalendarService calendarService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nova_consulta);
Calendar calendar = Calendar.getInstance();
ano = calendar.get(Calendar.YEAR);
mes = calendar.get(Calendar.MONTH);
dia = calendar.get(Calendar.DAY_OF_MONTH);
dataButton = (Button) findViewById(R.id.data);
tipo_consultaET = (EditText) findViewById(R.id.tipo_consulta);
localET = (EditText) findViewById(R.id.local);
telefoneET = (EditText) findViewById(R.id.telefone);
enderecoET = (EditText) findViewById(R.id.endereco);
dao = new AvisaSusDAO(this);
id = getIntent().getLongExtra(Constantes.CONSULTA_SELECIONADA, -1);
if(id != -1){
prepararEdicao();
}
calendarService = criarCalendarService();
}
private CalendarService criarCalendarService() {
SharedPreferences preferencias =
getSharedPreferences(PREFERENCIAS, MODE_PRIVATE);
String nomeConta = preferencias.getString(NOME_CONTA, null);
String tokenAcesso = preferencias.getString(TOKEN_ACESSO, null);
return new CalendarService(nomeConta, tokenAcesso);
}
private void prepararEdicao() {
ConsultaD consulta = dao.buscarViagemPorId(id);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
tipo_consultaET.setText(consulta.getTipo_consulta());
data = consulta.getDia_consulta();
dataButton.setText(dateFormat.format(data));
telefoneET.setText(consulta.getTelefone().toString());
enderecoET.setText(consulta.getEndereco());
}
public void selecionarData(View view) {
showDialog(view.getId());
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case R.id.data:
return new DatePickerDialog(this, dataConsulta, ano, mes, dia);
}
return null;
}
OnDateSetListener dataConsulta = new OnDateSetListener() {
public void onDateSet(DatePicker view, int anoSelecionado, int mesSelecionado, int diaSelecionado) {
data = criarData(anoSelecionado, mesSelecionado, diaSelecionado);
dataButton.setText(dia + "/" + (mes + 1) + "/" + ano);
}
};
private Date criarData(int anoSelecionado, int mesSelecionado, int diaSelecionado) {
Calendar calendar = Calendar.getInstance();
calendar.set(anoSelecionado, mesSelecionado, diaSelecionado);
return calendar.getTime();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.consulta_menu, menu);
return true;
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case R.id.nova_consulta:
startActivity(new Intent(this, NovaConsulta.class));
return true;
case R.id.remover:
removerConsulta(Long.valueOf(id));
finish();
return true;
default:
return super.onMenuItemSelected(featureId, item);
}
}
private void removerConsulta(Long id) {
dao.removerConsulta(id);
}
public void salvarViagem(View view){
DatabaseHelper.Consulta consulta = new DatabaseHelper.Consulta();
consulta.setTipo_consulta(tipo_consultaET.getText().toString());
consulta.setDia_consulta(data);
consulta.setTelefone(telefoneET.getText().toString());
consulta.setEndereco(enderecoET.getText().toString());
long resultado;
if(id == -1){
resultado = dao.inserir(consulta);
new Task().execute(consulta);
}else{
resultado = dao.atualizar(consulta);
}
if(resultado != -1 ){
Toast.makeText(this, getString(R.string.registro_salvo), Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, getString(R.string.erro_salvar), Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onDestroy() {
dao.close();
super.onDestroy();
}
private class Task extends AsyncTask<ConsultaD, Void, Void>{
@Override
protected Void doInBackground(ConsultaD... consultas) {
ConsultaD consulta = consultas[0];
calendarService.criarEvento(consulta);
return null;
}
}
}
Java query. :
public class ConsultaD {
private Long id;
private String tipo_consulta;
private Date dia_consulta;
private String local;
private String telefone;
private String endereco;
private String latitude;
private String longitude;
private String situacao;
private String descricao;
public ConsultaD(Long id, String tipo_consulta, Date dia_consulta, String local, String telefone, String endereco,
String latitude, String longitude, String situacao, String descricao) {
this.id = id;
this.tipo_consulta = tipo_consulta;
this.dia_consulta = dia_consulta;
this.local = local;
this.telefone = telefone;
this.endereco = endereco;
this.latitude = latitude;
this.longitude = longitude;
this.situacao = situacao;
this.descricao = descricao;
}
public ConsultaD() {
}
public Long getId() {return id;}
public void setId(Long id) {this.id = id;}
public String getTipo_consulta(){return tipo_consulta;}
public void setTipo_consulta(String tipo_consulta) {this.tipo_consulta = tipo_consulta;}
public Date getDia_consulta() {return dia_consulta;}
public void setDia_consulta(Date dia_consulta) {this.dia_consulta = dia_consulta;}
public String getLocal(){return local;}
public void setLocal(String local) {this.local = local;}
public String getTelefone(){return telefone;}
public void setTelefone(String telefone) {this.telefone = telefone;}
public String getEndereco(){return endereco;}
public void setEndereco(String endereco) {this.endereco = endereco;}
public String getLatitude(){return latitude;}
public void setLatitude(String latitude) {this.latitude = latitude;}
public String getLongitude(){return longitude;}
public void setLongitude(String longitude) {this.longitude = longitude;}
public String getSituacao(){return situacao;}
public void setSituacao(String situacao) {this.situacao = situacao;}
public String getDescricao(){return descricao;}
public void setDescricao(String descricao) {this.descricao = descricao;}
}
Warned.java. :
public class AvisaDAO {
private DatabaseHelper helper;
private SQLiteDatabase db;
public AvisaDAO(Context context){
helper = new DatabaseHelper(context);
}
private SQLiteDatabase getDb() {
if (db == null) {
db = helper.getWritableDatabase();
}
return db;
}
public void close(){
helper.close();
db = null;
}
public List<DatabaseHelper.Consulta> listarConsulta(){
Cursor cursor = getDb().query(DatabaseHelper.Consulta.TABELA,
DatabaseHelper.Consulta.COLUNAS,
null, null, null, null, null);
List<DatabaseHelper.Consulta> viagens = new ArrayList<DatabaseHelper.Consulta>();
while(cursor.moveToNext()){
DatabaseHelper.Consulta viagem = criarConsulta(cursor);
viagens.add(viagem);
}
cursor.close();
return viagens;
}
public DatabaseHelper.Consulta buscarViagemPorId(Long id){
Cursor cursor = getDb().query(DatabaseHelper.Consulta.TABELA,
DatabaseHelper.Consulta.COLUNAS,
DatabaseHelper.Consulta._ID + " = ?",
new String[]{ id.toString() },
null, null, null);
if(cursor.moveToNext()){
DatabaseHelper.Consulta consulta = criarConsulta(cursor);
cursor.close();
return consulta;
}
return null;
}
public long inserir(ConsultaD consulta){
ContentValues values = new ContentValues();
values.put(DatabaseHelper.Consulta.VL_ENDERECO,
consulta.getEndereco());
values.put(DatabaseHelper.Consulta.VL_LATITUDE,
consulta.getLatitude());
values.put(DatabaseHelper.Consulta.NM_LOCAL,
consulta.getLocal());
values.put(DatabaseHelper.Consulta.DM_SITUACAO,
consulta.getSituacao());
values.put(DatabaseHelper.Consulta.VL_LONGITUDE,
consulta.getLongitude());
values.put(DatabaseHelper.Consulta.VL_TELEFONE,
consulta.getTelefone());
values.put(DatabaseHelper.Consulta.TIPO_CONSULTA,
consulta.getTipo_consulta());
values.put(DatabaseHelper.Consulta.DIA_CONSULTA,
consulta.getDia_consulta().toString());
values.put(DatabaseHelper.Consulta.VL_DESCRICAO,
consulta.getDescricao());
return getDb().insert(DatabaseHelper.Consulta.TABELA,
null, values);
}
public long atualizar(ConsultaD consulta){
ContentValues values = new ContentValues();
values.put(DatabaseHelper.Consulta.VL_ENDERECO,
consulta.getEndereco());
values.put(DatabaseHelper.Consulta.VL_LATITUDE,
consulta.getLatitude());
values.put(DatabaseHelper.Consulta.NM_LOCAL,
consulta.getLocal());
values.put(DatabaseHelper.Consulta.DM_SITUACAO,
consulta.getSituacao());
values.put(DatabaseHelper.Consulta.VL_LONGITUDE,
consulta.getLongitude());
values.put(DatabaseHelper.Consulta.VL_TELEFONE,
consulta.getTelefone());
values.put(DatabaseHelper.Consulta.TIPO_CONSULTA,
consulta.getTipo_consulta());
values.put(DatabaseHelper.Consulta.DIA_CONSULTA,
consulta.getDia_consulta().toString());
values.put(DatabaseHelper.Consulta.VL_DESCRICAO,
consulta.getDescricao());
return getDb().update(DatabaseHelper.Consulta.TABELA,
values, DatabaseHelper.Consulta._ID + " = ?",
new String[]{consulta.getId().toString()});
}
public boolean removerConsulta(Long id){
String whereClause = DatabaseHelper.Consulta._ID + " = ?";
String[] whereArgs = new String[]{id.toString()};
int removidos = getDb().delete(DatabaseHelper.Consulta.TABELA,
whereClause, whereArgs);
return removidos > 0;
}
private DatabaseHelper.Consulta criarConsulta(Cursor cursor) {
DatabaseHelper.Consulta consulta = new DatabaseHelper.Consulta(
);
return consulta;
}
}
Databasehelper.java :
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String BANCO_DADOS = "Avisa";
private static int VERSAO = 1;
public static class Consulta extends ConsultaD {
public static final String TABELA = "Consulta";
public static final String _ID = "_id";
public static final String TIPO_CONSULTA = "tipo_consulta";
public static final String DIA_CONSULTA = "dia_consulta";
public static final String NM_LOCAL = "local_consulta";
public static final String VL_TELEFONE = "telefone";
public static final String VL_ENDERECO = "endereco";
public static final String VL_LATITUDE = "latitude";
public static final String VL_LONGITUDE = "longitude";
public static final String DM_SITUACAO = "situacao";
public static final String VL_DESCRICAO = "descricao";
public static final String[] COLUNAS = new String[]{
_ID, TIPO_CONSULTA, DIA_CONSULTA,NM_LOCAL,VL_TELEFONE,VL_ENDERECO,VL_LATITUDE,VL_LONGITUDE,DM_SITUACAO,VL_DESCRICAO };
public Consulta(Long id, String tipo_consulta, Date dia_consulta, String local, String telefone, String endereco, String latitude, String longitude, String situacao, String descricao) {
super(id, tipo_consulta, dia_consulta, local, telefone, endereco, latitude, longitude, situacao, descricao);
}
public Consulta() {
super();
}
}
public DatabaseHelper(Context context) {
super(context, BANCO_DADOS, null, VERSAO);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE TB_COMPROMISSO_APP (ID_COMPROMISSO_APP CHAR(18) PRIMARY KEY NOT NULL," +
" DT_COMPROMISSO DATE NOT NULL, NM_LOCAL VARCHAR(300) NULL, VL_TELEFONE VARCHAR(30) NOT NULL, VL_ENDERECO VARCHAR(300) NOT NULL," +
"VL_LATITUDE VARCHAR(30) NOT NULL, VL_LONGITUDE VARCHAR(30) NULL,DM_SITUACAO CHAR(18) NULL, VL_DESCRICAO VARCHAR(300) NULL");
db.execSQL("CREATE TABLE TB_USUARIO_APP (ID_USUARIO_APP CHAR(18) PRIMARY KEY NOT NULL," +
" VL_TELEFONE VARCHAR(30) NULL, VL_SENHA VARCHAR(30) NOT NULL, VL_EMAIL VARCHAR(30) NULL, NM_SOCIAL VARCHAR(300)" );
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE TB_COMPROMISSO_APP;");
db.execSQL("DROP TABLE TB_USUARIO_APP;");
onCreate(db);
}
}
Error:
06-01 09:39:45.974 4650-4650/br.avisasuscom.avisasus_v3 E/AndroidRuntime: FATAL EXCEPTION: main
Process: br.avisasuscom.avisasus_v3, PID: 4650
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3823)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3818)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at br.avisasuscom.avisasus_v3.dao.AvisaSusDAO.inserir(AvisaSusDAO.java:90)
at br.avisasuscom.avisasus_v3.NovaConsulta.salvarViagem(NovaConsulta.java:162)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3818)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Welcome to Sopt. Add the full stack of errors to the question.
– user28595
Okay, I put the bug that you see in the logcat, because it doesn’t show any errors apparently in the code. And I put up a print that’s what happens after you click save, instead of writing to the comic, it shows this message
– Thiago Luiz Ferreira
no seu log analise este trecho: 
 Caused by: java.lang.NullPointerException at br.avisasuscom.avisasus_v3.dao.AvisaSusDAO.inserir(AvisaSusDAO.java:90)
at br.avisasuscom.avisasus_v3.NovaConsulta.salvarViagem(NovaConsulta.java:162) put a breakpoint one line before those given lines and see if the method is receiving the data correctly. as it is not handling SQL excesses you will only be able to find what is going wrong for your debug database
– Marcela Melo