3
respostas

Aula - Adicionando Novos Destinos (Navigation Parte 01) - App quebra ao adicionar 2 placeholders

Segui o mesmo passo a passo das aulas, consigui adicionar os 2 placeholders e fazer a configuração deles normalmente. Porém quando fui executar o app, o mesmo quebra em tempo de execução. Código abaixo do nav_graph.xml

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/nav_graph"
    app:startDestination="@id/listaProdutos">
    <fragment
        tools:layout="@layout/lista_produtos"
        android:id="@+id/listaProdutos"
        android:name="br.com.alura.aluraesporte.ui.fragment.ListaProdutosFragment"
        android:label="Lista de produtos" />
    <fragment
        tools:layout="@layout/detalhes_produto"
        android:id="@+id/detalhesProduto"
        android:name="br.com.alura.aluraesporte.ui.fragment.DetalhesProdutoFragment"
        android:label="Detalhes do produto"
        />
</navigation>

código xml da activity do projeto (que irá hospedar vários fragments)

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

    <fragment
        android:id="@+id/produtos_activity_nav_host"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="androidx.navigation.fragment.NavHostFragment"
        app:navGraph="@navigation/nav_graph"
        app:defaultNavHost="true"/>
</FrameLayout>

código kotlin da activity principal

package br.com.alura.aluraesporte.ui.activity

import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import br.com.alura.aluraesporte.R
import br.com.alura.aluraesporte.ui.fragment.DetalhesProdutoFragment
import br.com.alura.aluraesporte.ui.fragment.ListaProdutosFragment
import br.com.alura.aluraesporte.ui.fragment.PagamentoFragment
import org.koin.android.ext.android.inject

private const val COMPRA_REALIZADA = "Compra realizada"

class ProdutosActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.produtos_activity)
        if (savedInstanceState == null) {
//            val produtosFragment: ListaProdutosFragment by inject()
//            transacaoFragment {
//                replace(R.id.container, produtosFragment)
//            }
        }
    }

    override fun onAttachFragment(fragment: Fragment?) {
        super.onAttachFragment(fragment)
        when (fragment) {
            is ListaProdutosFragment -> {
                fragment.quandoProdutoSelecionado = { produtoSelecionado ->
                    val detalhesProdutoFragment: DetalhesProdutoFragment by inject()
                    val argumentos = Bundle()
                    argumentos.putLong(CHAVE_PRODUTO_ID, produtoSelecionado.id)
                    detalhesProdutoFragment.arguments = argumentos
                    transacaoFragment {
                        addToBackStack(null)
                        replace(R.id.container, detalhesProdutoFragment)
                    }
                }
            }
            is DetalhesProdutoFragment -> {
                fragment.quandoProdutoComprado = { produtoComprado ->
                    val pagamentoFragment: PagamentoFragment by inject()
                    val dado = Bundle()
                    dado.putLong(CHAVE_PRODUTO_ID, produtoComprado.id)
                    pagamentoFragment.arguments = dado
                    transacaoFragment {
                        addToBackStack(null)
                        replace(R.id.container, pagamentoFragment)
                    }
                }
            }
            is PagamentoFragment -> {
                fragment.quandoPagamentoRealizado = {
                    Toast.makeText(this, COMPRA_REALIZADA, Toast.LENGTH_LONG).show()
                }
            }
        }
    }

}
3 respostas

Olhando o logcat com o filtro de Error, vi que está dando erro aparentemente na parte do Koin, mas nem mexi neste arquivo. ` 2022-04-10 15:48:20.839 16200-16200/br.com.alura.aluraesporte E/AndroidRuntime: FATAL EXCEPTION: main Process: br.com.alura.aluraesporte, PID: 16200 java.lang.RuntimeException: java.lang.reflect.InvocationTargetException at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) Caused by: org.koin.core.error.InstanceCreationException: Could not create instance for [type:Factory,primary_type:'br.com.alura.aluraesporte.ui.viewmodel.ProdutosViewModel'] at org.koin.core.instance.DefinitionInstance.create(DefinitionInstance.kt:61) at org.koin.core.instance.FactoryDefinitionInstance.get(FactoryDefinitionInstance.kt:37) at org.koin.core.definition.BeanDefinition.resolveInstance(BeanDefinition.kt:70) at org.koin.core.scope.Scope.resolveInstance(Scope.kt:165) at org.koin.core.scope.Scope.get(Scope.kt:128) at org.koin.android.viewmodel.ViewModelResolutionKt$createViewModelProvider$1.create(ViewModelResolution.kt:63) at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:135) at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:103) at org.koin.android.viewmodel.ViewModelResolutionKt.getInstance(ViewModelResolution.kt:40) at org.koin.android.viewmodel.ViewModelResolutionKt.getViewModel(ViewModelResolution.kt:20) at org.koin.android.viewmodel.ext.android.LifecycleOwnerExtKt.getViewModel(LifecycleOwnerExt.kt:85) at br.com.alura.aluraesporte.ui.fragment.ListaProdutosFragment$special$$inlined$viewModel$default$1.invoke(LifecycleOwnerExt.kt:95) at br.com.alura.aluraesporte.ui.fragment.ListaProdutosFragment$special$$inlined$viewModel$default$1.invoke(Unknown Source:0) at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74) at br.com.alura.aluraesporte.ui.fragment.ListaProdutosFragment.getViewModel(ListaProdutosFragment.kt:21) at br.com.alura.aluraesporte.ui.fragment.ListaProdutosFragment.buscaProdutos(ListaProdutosFragment.kt:31) at br.com.alura.aluraesporte.ui.fragment.ListaProdutosFragment.onCreate(ListaProdutosFragment.kt:27) at androidx.fragment.app.Fragment.performCreate(Fragment.java:2414) at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1418) at androidx.fragment.app.FragmentManagerImpl.addAddedFragments(FragmentManager.java:2646) at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2416) at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2372) at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273) at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3273) at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3229) at androidx.fragment.app.Fragment.performActivityCreated(Fragment.java:2466) at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1483) at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784) at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852) at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3269) at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3229) at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:201) at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:620) at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:178) at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1435) at android.app.Activity.performStart(Activity.java:8018) at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3475)

    at androidx.fragment.app.Fragment.performActivityCreated(Fragment.java:2466)

2022-04-10 15:48:20.844 16200-16200/br.com.alura.aluraesporte E/AndroidRuntime: at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221) at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201) at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7656) ... 3 more Caused by: org.koin.core.error.InstanceCreationException: Could not create instance for [type:Single,primary_type:'br.com.alura.aluraesporte.repository.ProdutoRepository'] at org.koin.core.instance.DefinitionInstance.create(DefinitionInstance.kt:61) at org.koin.core.instance.SingleDefinitionInstance.get(SingleDefinitionInstance.kt:40) at org.koin.core.definition.BeanDefinition.resolveInstance(BeanDefinition.kt:70) at org.koin.core.scope.Scope.resolveInstance(Scope.kt:165) at org.koin.core.scope.Scope.get(Scope.kt:128) at br.com.alura.aluraesporte.di.AppModulesKt$viewModelModule$1$1.invoke(AppModules.kt:95) at br.com.alura.aluraesporte.di.AppModulesKt$viewModelModule$1$1.invoke(AppModules.kt:88) at org.koin.core.instance.DefinitionInstance.create(DefinitionInstance.kt:54) ... 47 more Caused by: org.koin.core.error.InstanceCreationException: Could not create instance for [type:Single,primary_type:'br.com.alura.aluraesporte.database.dao.ProdutoDAO'] at org.koin.core.instance.DefinitionInstance.create(DefinitionInstance.kt:61) at org.koin.core.instance.SingleDefinitionInstance.get(SingleDefinitionInstance.kt:40) at org.koin.core.definition.BeanDefinition.resolveInstance(BeanDefinition.kt:70) at org.koin.core.scope.Scope.resolveInstance(Scope.kt:165) at org.koin.core.scope.Scope.get(Scope.kt:128) at br.com.alura.aluraesporte.di.AppModulesKt$daoModule$1$3.invoke(AppModules.kt:95) at br.com.alura.aluraesporte.di.AppModulesKt$daoModule$1$3.invoke(AppModules.kt:76) at org.koin.core.instance.DefinitionInstance.create(DefinitionInstance.kt:54) ... 54 more Caused by: org.koin.core.error.InstanceCreationException: Could not create instance for [type:Single,primary_type:'br.com.alura.aluraesporte.database.AppDatabase'] at org.koin.core.instance.DefinitionInstance.create(DefinitionInstance.kt:61) at org.koin.core.instance.SingleDefinitionInstance.get(SingleDefinitionInstance.kt:40) at org.koin.core.definition.BeanDefinition.resolveInstance(BeanDefinition.kt:70) at org.koin.core.scope.Scope.resolveInstance(Scope.kt:165) at org.koin.core.scope.Scope.get(Scope.kt:128) at br.com.alura.aluraesporte.di.AppModulesKt$daoModule$1$1.invoke(AppModules.kt:95) at br.com.alura.aluraesporte.di.AppModulesKt$daoModule$1$1.invoke(AppModules.kt:74) at org.koin.core.instance.DefinitionInstance.create(DefinitionInstance.kt:54) ... 61 more Caused by: java.lang.RuntimeException: cannot find implementation for br.com.alura.aluraesporte.database.AppDatabase. AppDatabase_Impl does not exist at androidx.room.Room.getGeneratedImplementation(Room.java:94) at androidx.room.RoomDatabase$Builder.build(RoomDatabase.java:667) at br.com.alura.aluraesporte.di.AppModulesKt$testeDatabaseModule$1$1.invoke(AppModules.kt:59) at br.com.alura.aluraesporte.di.AppModulesKt$testeDatabaseModule$1$1.invoke(AppModules.kt:30) at org.koin.core.instance.DefinitionInstance.create(DefinitionInstance.kt:54) ... 68 more

Opa Wilkison, blz? Dado que comentou que o Koin alertou o problema, seria mais fácil de identificar testando o projeto, você poderia enviar o projeto via GitHub, por favor?