o erro que da é esse : FATAL EXCEPTION: main Process: com.app.felipe.agenda, PID: 15910 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.felipe.agenda/com.app.felipe.agenda.FormularioActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
Activity da lista de alunos :
package com.app.felipe.agenda;
import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import android.support.v4.app.ActivityCompat; 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 com.app.felipe.agenda.DAO.AlunoDAO; import com.app.felipe.agenda.Modelo.Aluno; import com.app.felipe.agenda.adpter.AlunosAdapter;
import java.io.Serializable; import java.util.List; import java.util.jar.Manifest;
public class ListaAlunosActivity extends AppCompatActivity {
private ListView ListaAlunos;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_lista_alunos); // xml da lista alunos ListaAlunos = (ListView) findViewById(R.id.lista_alunos);
//implementando botão de edição do 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);
} });
//botão para adicionar novo aluno com xml Button NovoAluno = (Button) findViewById(R.id.lista_alunos_NovoContato); assert NovoAluno != null; NovoAluno.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent VaiProFormulario = new Intent(ListaAlunosActivity.this,FormularioActivity.class); startActivity(VaiProFormulario); } });
//resgistrando um menu de contexto registerForContextMenu(ListaAlunos); }
private void atualizaLista() { AlunoDAO dao = new AlunoDAO(this); List lista =dao.buscaAlunos(); dao.close();
AlunosAdapter adapter= new AlunosAdapter(this,lista); ListaAlunos.setAdapter(adapter); }
@Override protected void onResume() { super.onResume();
atualizaLista(); }
@Override public void onCreateContextMenu(ContextMenu menu, View v, final ContextMenu.ContextMenuInfo menuInfo) {
//pegando posição do aluno e o proprio AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo; final Aluno aluno = (Aluno) ListaAlunos.getItemAtPosition(info.position);
//ligar MenuItem vaiPraLigar = menu.add("Ligar"); vaiPraLigar.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { if (ActivityCompat.checkSelfPermission(ListaAlunosActivity.this, android.Manifest.permission.CALL_PHONE)!= PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(ListaAlunosActivity.this, new String[]{android.Manifest.permission.CALL_PHONE},123); }else{ Intent intentLigar = new Intent(Intent.ACTION_CALL); String Ligar = "tel:"+aluno.getTelefone(); intentLigar.setData(Uri.parse(Ligar)); startActivity(intentLigar); }
return false; } });
//enviando SMS MenuItem vaiProSMS = menu.add("enviar SMS"); Intent intentSMS = new Intent(Intent.ACTION_VIEW); String SMS = "sms:"+aluno.getTelefone(); intentSMS.setData(Uri.parse(SMS)); vaiProSMS.setIntent(intentSMS);
//buscando localização no mapa MenuItem vaiProMapa = menu.add("Ver contato no mapa"); Intent intentMapa = new Intent(Intent.ACTION_VIEW); String Mapa = "geo:0,0?z=14&q="+aluno.getEndereco(); intentMapa.setData(Uri.parse(Mapa)); vaiProMapa.setIntent(intentMapa);
//enviando email MenuItem vaiProEmail = menu.add("Enviar email"); Intent intentEmail = new Intent(Intent.ACTION_VIEW); String email = "mailto:"+aluno.getEmail(); intentEmail.setData(Uri.parse(email)); vaiProEmail.setIntent(intentEmail);
//deletando contato 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); Toast.makeText(ListaAlunosActivity.this,"aluno " + aluno.getNome() + " deletado",Toast.LENGTH_SHORT).show(); AlunoDAO dao = new AlunoDAO(ListaAlunosActivity.this); dao.deletaAluno(aluno); dao.close(); atualizaLista(); return false; } });
} }
a classe do adapter: package com.app.felipe.agenda.adpter;
import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView;
import com.app.felipe.agenda.ListaAlunosActivity; import com.app.felipe.agenda.Modelo.Aluno;
import java.util.List;
/* Created by Felipe on 18/05/2016. */ public class AlunosAdapter extends BaseAdapter { private final List alunos; private final Context context;
public AlunosAdapter(Context context, List lista) { this.context = context; this.alunos = lista; }
@Override public int getCount() { return alunos.size(); }
@Override public Object getItem(int position) { return alunos.get(position); }
@Override public long getItemId(int position) { return alunos.get(position).getId(); }
@Override public View getView(int position, View convertView, ViewGroup parent) { TextView view = new TextView(context); Aluno aluno = alunos.get(position); view.setText(aluno.toString()); return view; } }