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

Agenda fecha ao clicar em botão de provas

A agenda fecha ao clicar no botão provas. O codigo funciona até o momento que é criado o fragment. Depois disso sempre que tento chamar a tela ProvasActivity a agenda para.

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: br.com.alura.agenda, PID: 7846
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.alura.agenda/br.com.alura.agenda.ProvasActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f090021
                      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: android.content.res.Resources$NotFoundException: Resource ID #0x7f090021
                      at android.content.res.Resources.getValue(Resources.java:1351)
                      at android.content.res.Resources.loadXmlResourceParser(Resources.java:2774)
                      at android.content.res.Resources.getLayout(Resources.java:1165)
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:421)
                      at br.com.alura.agenda.ListaProvasFragment.onCreateView(ListaProvasFragment.java:26)
                      at android.support.v4.app.Fragment.performCreateView(Fragment.java:2439)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
                      at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
                      at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
                      at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
                      at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
                      at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
                      at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
                      at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3273)
                      at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3229)
                      at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
                      at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:620)
                      at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
                      at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1237)
                      at android.app.Activity.performStart(Activity.java:6253)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
package br.com.alura.agenda;


import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;


public class ProvasActivity extends AppCompatActivity {

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

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction tx = fragmentManager.beginTransaction();
        tx.replace(R.id.frame_principal, new ListaProvasFragment());
        tx.commit();
    }
}
2 respostas
solução!

ListaProvasFragment

package br.com.alura.agenda;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.Arrays;
import java.util.List;

import br.com.alura.agenda.modelo.Prova;

public class ListaProvasFragment extends Fragment{

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_lista_provas, container, false);

        List<String> topPortugues = Arrays.asList("Adverbio", "Sujeito", "Figuras de Linguagem");
        Prova provaPortugues = new Prova("Portugues", "20/04/19", topPortugues);

        List<String> topMatematica = Arrays.asList("Equação 1", "Algebra booleana", "Algebra linear");
        Prova provaMatematica = new Prova("Matematica", "22/04/19", topMatematica);

        List<Prova> provas = Arrays.asList(provaMatematica, provaPortugues);

        ArrayAdapter<Prova> adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1, provas);

        ListView lista = (ListView) view.findViewById(R.id.provas_lista);
        lista.setAdapter(adapter);

        lista.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Prova prova = (Prova) parent.getItemAtPosition(position);
                Toast.makeText(getContext(), "Clicou na prova de "+ prova, Toast.LENGTH_SHORT).show();
                Intent vaiParaDetalhes = new Intent(getContext(), DetalhesProvaActivity.class); //Instancia a activity DetalhesProvaActivity
                vaiParaDetalhes.putExtra("prova", prova);
                startActivity(vaiParaDetalhes); // Inicia a activity e vai para tela DetalhesProvaActivity

            }
        });

        return view;
    }
}

activity_provas.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/frame_principal">

</FrameLayout>

Consegui resolver após rever as ultimas aulas umas duas vezes e revisar o código por umas 3 horas.

Acabei trocando na hora de copiar o conteúdo de

activity_provas.xml

para

fragment_lista_prova.xml

e

fragment_detalhes_prova.xml

O código compilava mas na hora de passar os valores para o ListView lista ele recebia valor nulo, pois a id passada por parâmetro apontava para o xml errado.

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