Sempre que eu abro o formulário de cadastro de alunos o da crash no app e eu recebo esse erro no Studio:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: br.com.alura.agenda, PID: 25523
java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.alura.agenda/br.com.alura.agenda.FormularioActivity}: java.lang.ClassCastException: android.support.v7.widget.AppCompatImageView cannot be cast to android.widget.Button
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatImageView cannot be cast to android.widget.Button
at br.com.alura.agenda.FormularioActivity.onCreate(FormularioActivity.java:36)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Application terminated.
E o código do Formulário:
package br.com.alura.agenda;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.File;
import br.com.alura.agenda.dao.AlunoDAO;
import br.com.alura.agenda.modelo.Aluno;
public class FormularioActivity extends AppCompatActivity {
private FormularioHelper helper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_formulario);
final Intent intent = getIntent();
Aluno aluno = (Aluno) intent.getSerializableExtra("aluno");
helper = new FormularioHelper(this);
if(aluno!=null){
helper.preencheFormulario(aluno);
}
Button botaoFoto = (Button)findViewById(R.id.formulario_botao_foto);
botaoFoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intentCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String caminhoFoto = getExternalFilesDir(null) +"/"+System.currentTimeMillis() +".jpg";
File arquivoFoto = new File(caminhoFoto);
intentCamera.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(arquivoFoto));
startActivity(intentCamera);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflar = getMenuInflater();
inflar.inflate(R.menu.menu_formulario, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.menu_formulario_ok:
Aluno aluno = helper.getAluno();
AlunoDAO alunoDAO= new AlunoDAO(this);
if(aluno.getId() == null) {
alunoDAO.insere(aluno);
} else {
alunoDAO.altera(aluno);
}
alunoDAO.close();
Toast.makeText(FormularioActivity.this, "Aluno "+aluno.getNome()+" Salvo", Toast.LENGTH_SHORT).show();
finish();
break;
}
return super.onOptionsItemSelected(item);
}
}