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

Erro no setAdapter no fragment

Estou com o seguinte erro:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: br.com.cadastroDeAluno, PID: 12066
                  java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
                      at br.com.cadastroDeAluno.ListarMediasFragment.carregaMediaAlunos(ListarMediasFragment.java:43)
                      at br.com.cadastroDeAluno.ListarMediasFragment.onResume(ListarMediasFragment.java:49)
                      at android.support.v4.app.Fragment.performResume(Fragment.java:2126)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1151)
                      at android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(FragmentManager.java:945)
                      at android.support.v4.app.FragmentManagerImpl.startPendingDeferredFragments(FragmentManager.java:1314)
                      at android.support.v4.app.FragmentManagerImpl.doPendingDeferredStart(FragmentManager.java:1700)
                      at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1641)
                      at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:679)
                      at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
                      at android.support.v4.view.ViewPager.populate(ViewPager.java:1240)
                      at android.support.v4.view.ViewPager.populate(ViewPager.java:1088)
                      at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1614)
                      at android.view.View.measure(View.java:19169)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6113)
                      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1723)
                      at android.widget.LinearLayout.measureVertical(LinearLayout.java:788)
                      at android.widget.LinearLayout.onMeasure(LinearLayout.java:648)
                      at android.view.View.measure(View.java:19169)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6113)
                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:223)
                      at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
                      at android.view.View.measure(View.java:19169)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6113)
                      at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:389)
                      at android.view.View.measure(View.java:19169)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6113)
                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:223)
                      at android.view.View.measure(View.java:19169)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6113)
                      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1723)
                      at android.widget.LinearLayout.measureVertical(LinearLayout.java:788)
                      at android.widget.LinearLayout.onMeasure(LinearLayout.java:648)
                      at android.view.View.measure(View.java:19169)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6113)
                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:223)
                      at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2720)
                      at android.view.View.measure(View.java:19169)
                      at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2481)
                      at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1440)
                      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1694)
                      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1321)
                      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6737)
                      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:894)
                      at android.view.Choreographer.doCallbacks(Choreographer.java:696)
                      at android.view.Choreographer.doFrame(Choreographer.java:631)
                      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:880)
                      at android.os.Handler.handleCallback(Handler.java:815)
                      at android.os.Handler.dispatchMessage(Handler.java:104)
                      at android.os.Looper.loop(Looper.java:207)
                      at android.app.ActivityThread.main(ActivityThread.java:5776)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
                      at com.android.internal.os.ZygoteInit.main(ZygoteIni
I/Process: Sending signal. PID: 12066 SIG: 9
Application terminated.

fragment:

package br.com.cadastroDeAluno;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import java.util.List;

import br.com.cadastroDeAluno.adapter.MediaDasNotasAdapter;
import br.com.cadastroDeAluno.dao.AlunoDAO;
import br.com.cadastroDeAluno.modelo.MediaDasNotas;


/**
 * A simple {@link Fragment} subclass.
 */
public class ListarMediasFragment extends Fragment {

    private ListView listaDasMediaAlunos;

    /**
     *    public ListarMediasFragment() {
     // Required empty public constructor
     }
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_listar_medias, container, false);
    }

    public void carregaMediaAlunos() {
        AlunoDAO daoMedia = new AlunoDAO(getActivity());
        List<MediaDasNotas> media = daoMedia.buscaMediaAlunos();
        daoMedia.close();
        //criando um MediaDasNotasAdapter
        MediaDasNotasAdapter adapterMedia = new MediaDasNotasAdapter(getActivity(), media);
        listaDasMediaAlunos.setAdapter(adapterMedia);
    }

    @Override
    public void onResume() {
        super.onResume();
        carregaMediaAlunos();
    }
}
2 respostas
solução!

Olá Leandro,

O erro que você está tomando é um NullPointerException quando você tenta invocar o método setAdapter(...) no atributo listaDasMediasAlunos. Qual o valor desse atributo se ele não ainda não foi inicializado?

Perceba que aqui o problema é bem parecido com o outro problema que você teve ao fazer o outro fragment. Tente lembrar como resolver, como fazer para inicializar a listaDasMediasAlunos antes de fazer o setAdapter.

realmente. era isso também.

estou muito feliz por poder contar com você e o Alura!

super satisfeito!!