Solucionado (ver solução)
Solucionado
(ver solução)
6
respostas

5. Persistência com SQLite

Percebi que houve uma mudança em algumas partes do código da classe java ListaAlunosActivity em relação ao que vinha seguindo de uma Instrutora nas aulas anteriores, e na Aula 5 foi um Instrutor diferente. Para seguir de forma correta os exercícios gostaria de saber se é possível passar o código que o Instrutor da Aula 5 fez para dar sequencia corretamente na Aula 6.

// Codigo atual  ListaAlunosActivity.java -

package br.com.the7web.cadastro;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.List;

/**
 * Created by J r n i n on 16/11/2015.
 */
public class ListaAlunosActivity extends AppCompatActivity {

    private AdapterView<ArrayAdapter<Aluno>> lista;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listagem_alunos);

        ListView lista= (ListView)findViewById(R.id.lista);

        lista.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapter, View view, int posicao, long id) {
                Toast.makeText(ListaAlunosActivity.this, "A posição é " + posicao, Toast.LENGTH_SHORT).show();
            }
        });
        lista.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapter, View view, int posicao, long id) {


                Toast.makeText(ListaAlunosActivity.this, "Aluno clicado é "+ alunos, Toast.LENGTH_SHORT).show();
                return true;
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_lista_alunos, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.novo:
                Intent irParaFormulario = new Intent(this, br.com.the7web.cadastro.FormularioActivity.class);
                startActivity(irParaFormulario);
                break;

            default:
                break;
        }

        return super.onOptionsItemSelected(item);
    }
    @Override
    protected void onResume(){
        super.onResume();
        Log.i("CICLO DE VIDA", "onResume");

        AlunoDAO dao = new AlunoDAO(this);
        List<Aluno> alunos = dao.getLista();

        ArrayAdapter<Aluno> adapter =
                new ArrayAdapter<Aluno>(this, android.R.layout.simple_list_item_1, alunos);
                lista.setAdapter(adapter);
    }
}

Ao abrir o APP aparece: Unfortunately, CadastroCaelum has stopped.

6 respostas

Oi Jose, tudo bem?

Eu só tenho o projeto completo, mas o que me lembro de cara que muda é que no vídeo da aula onde é criado o layout do formulário, é utilizado um RatingBar para a nota do aluno, e agora nessa aula ele utiliza um SeekBar, talvez seja isso que esteja causando problemas no seu projeto, mas pode ser outra coisa. Se você puder copiar a parte do log onde aparece o erro, seria bom.

Vamos tentando descobrir o que é então. :)

No layout formulario.xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/foto"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:src="@drawable/ic_launcher"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Nome:" />

        <EditText
            android:id="@+id/nome"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Site:" />

        <EditText
            android:id="@+id/site"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Endereço:" />

        <EditText
            android:id="@+id/endereco"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Telefone:" />

        <EditText
            android:id="@+id/telefone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Nota:" />

        <SeekBar
            android:id="@+id/nota"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="10"/>

        <Button
            android:id="@+id/botao"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Inserir"/>
    </LinearLayout>
</ScrollView>

Aqui tem meu FormularioHelper onde você pode ver como recuperar e trabalhar com o SeekBar:

public class FormularioHelper {

    private EditText campoNome;
    private EditText campoTelefone;
    private EditText campoEndereco;
    private EditText campoSite;
    private SeekBar campoNota;
    private Aluno aluno;

    public FormularioHelper(FormularioActivity activity) {
        aluno = new Aluno();

        campoNome = (EditText) activity.findViewById(R.id.nome);
        campoTelefone = (EditText) activity.findViewById(R.id.telefone);
        campoEndereco = (EditText) activity.findViewById(R.id.endereco);
        campoSite = (EditText) activity.findViewById(R.id.site);
        campoNota = (SeekBar) activity.findViewById(R.id.nota);
    }

    public Aluno pegaAlunoDoFormulario() {
        String nome = campoNome.getText().toString();
        String telefone = campoTelefone.getText().toString();
        String endereco = campoEndereco.getText().toString();
        String site = campoSite.getText().toString();
        int nota = campoNota.getProgress();

        aluno.setNome(nome);
        aluno.setTelefone(telefone);
        aluno.setEndereco(endereco);
        aluno.setSite(site);
        aluno.setNota(Double.valueOf(nota));

        return aluno;
    }

    public void colocaAlunoNoFormulario(Aluno alunoParaSerAlterado) {
        campoNome.setText(alunoParaSerAlterado.getNome());
        campoTelefone.setText(alunoParaSerAlterado.getTelefone());
        campoEndereco.setText(alunoParaSerAlterado.getEndereco());
        campoSite.setText(alunoParaSerAlterado.getSite());
        campoNota.setProgress(alunoParaSerAlterado.getNota().intValue());

        aluno.setId(alunoParaSerAlterado.getId());
    }
}

Posta se funcionou fazendo a troca, ou se tem alguma dúvida quanto a isso, que aí vamos tentando até resolver.

Abraço!

Ao abrir o APP aparece: Unfortunately, CadastroCaelum has stopped.

//Log
11-17 17:32:34.921 32674-32674/? I/art: Not late-enabling -Xcheck:jni (already on)
11-17 17:32:35.033 32674-32674/? W/Zygote: Slow operation: 1298ms so far, now at Zygote.nativeForkAndSpecialize
11-17 17:32:35.284 32674-32674/? W/Zygote: Slow operation: 1550ms so far, now at Zygote.postForkCommon
11-17 17:32:35.285 32674-32674/? W/Zygote: Slow operation: 1582ms so far, now at zygoteConnection.runOnce: postForkAndSpecialize
11-17 17:32:37.013 32674-32681/br.com.the7web.cadastro W/art: Suspending all threads took: 104.660ms
11-17 17:32:37.564 32674-32681/br.com.the7web.cadastro W/art: Suspending all threads took: 156.166ms
11-17 17:32:38.014 32674-32681/br.com.the7web.cadastro W/art: Suspending all threads took: 105.293ms
11-17 17:32:38.430 32674-32681/br.com.the7web.cadastro W/art: Suspending all threads took: 19.892ms
11-17 17:32:38.939 32674-32681/br.com.the7web.cadastro W/art: Suspending all threads took: 26.027ms
11-17 17:32:39.762 32674-32674/br.com.the7web.cadastro D/AndroidRuntime: Shutting down VM
11-17 17:32:39.819 32674-32674/br.com.the7web.cadastro E/AndroidRuntime: FATAL EXCEPTION: main
11-17 17:32:39.819 32674-32674/br.com.the7web.cadastro E/AndroidRuntime: Process: br.com.the7web.cadastro, PID: 32674
11-17 17:32:39.819 32674-32674/br.com.the7web.cadastro E/AndroidRuntime: java.lang.RuntimeException: Unable to resume activity {br.com.the7web.cadastro/br.com.the7web.cadastro.ListaAlunosActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
11-17 17:32:39.819 32674-32674/br.com.the7web.cadastro E/AndroidRuntime:     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2989)
11-17 17:32:39.819 32674-32674/br.com.the7web.cadastro E/AndroidRuntime:     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3020)
11-17 17:32:39.819 32674-32674/br.com.the7web.cadastro E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
11-17 17:32:39.819 32674-32674/br.com.the7web.cadastro E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:151)
11-17 17:32:39.819 32674-32674/br.com.the7web.cadastro E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
11-17 17:32:39.819 32674-32674/br.com.the7web.cadastro E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
11-17 17:32:39.819 32674-32674/br.com.the7web.cadastro E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135)
11-17 17:32:39.819 32674-32674/br.com.the7web.cadastro E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5257)
11-17 17:32:39.819 32674-32674/br.com.the7web.cadastro E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
11-17 17:32:39.819 32674-32674/br.com.the7web.cadastro E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
11-17 17:32:39.819 32674-32674/br.com.the7web.cadastro E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
11-17 17:32:39.819 32674-32674/br.com.the7web.cadastro E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
11-17 17:32:39.819 32674-32674/br.com.the7web.cadastro E/AndroidRuntime:  Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
11-17 17:32:39.819 32674-32674/br.com.the7web.cadastro E/AndroidRuntime:     at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)
11-17 17:32:39.819 32674-32674/br.com.the7web.cadastro E/AndroidRuntime:     at android.widget.ListView.setAdapter(ListView.java:487)
11-17 17:32:39.819 32674-32674/br.com.the7web.cadastro E/AndroidRuntime:     at br.com.the7web.cadastro.ListaAlunosActivity.carregaLista(ListaAlunosActivity.java:71)
11-17 17:32:39.819 32674-32674/br.com.the7web.cadastro E/AndroidRuntime:     at br.com.the7web.cadastro.ListaAlunosActivity.onResume(ListaAlunosActivity.java:64)
11-17 17:32:39.819 32674-32674/br.com.the7web.cadastro E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1257)
11-17 17:32:39.819 32674-32674/br.com.the7web.cadastro E/AndroidRuntime:     at android.app.Activity.performResume(Activity.java:6076)
11-17 17:32:39.819 32674-32674/br.com.the7web.cadastro E/AndroidRuntime:     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2978)
11-17 17:32:39.819 32674-32674/br.com.the7web.cadastro E/AndroidRuntime:     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3020) 
11-17 17:32:39.819 32674-32674/br.com.the7web.cadastro E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395) 
11-17 17:32:39.819 32674-32674/br.com.the7web.cadastro E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:151) 
11-17 17:32:39.819 32674-32674/br.com.the7web.cadastro E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
11-17 17:32:39.819 32674-32674/br.com.the7web.cadastro E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
11-17 17:32:39.819 32674-32674/br.com.the7web.cadastro E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135) 
11-17 17:32:39.819 32674-32674/br.com.the7web.cadastro E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5257) 
11-17 17:32:39.819 32674-32674/br.com.the7web.cadastro E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
11-17 17:32:39.819 32674-32674/br.com.the7web.cadastro E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 
11-17 17:32:39.819 32674-32674/br.com.the7web.cadastro E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
11-17 17:32:39.819 32674-32674/br.com.the7web.cadastro E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
11-17 17:32:39.867 32674-32686/br.com.the7web.cadastro I/art: Background sticky concurrent mark sweep GC freed 3108(262KB) AllocSpace objects, 0(0B) LOS objects, 27% free, 813KB/1117KB, paused 11.691ms total 298.338ms
11-17 17:32:39.891 32674-32686/br.com.the7web.cadastro W/art: Suspending all threads took: 20.563ms
11-17 17:32:43.478 32674-32681/br.com.the7web.cadastro W/art: Suspending all threads took: 50.922ms
11-17 17:32:45.023 32674-32674/br.com.the7web.cadastro I/Process: Sending signal. PID: 32674 SIG: 9

Oi Jose, tudo bem?

Você quis encerrar mesmo o tópico? Já conseguiu resolver?

Abraço.

Lucas encerrei, mas não resolvi, ai abri outro na tentativa de uma solução...se puder me ajudar mais uma vez agradeço!

solução!

Oi Jose, vou interagir lá no outro. Vamos tentar resolver sim!

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software