Caused by: java.lang.Classcastexception: android.widget.Framelayout cannot be cast to androidx.drawerlayout.widget.Drawerlayout

Asked

Viewed 27 times

-1

I’m trying to create a Drawer navigation menu, but when I run my app it crashes, already searched and the changes that were to be made at first I already did. Someone can help me?

Error log:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.pokedex, PID: 7212
java.lang.RuntimeException: Unable to start activity 
ComponentInfo{com.example.pokedex/com.example.pokedex.ui.main.MainActivity}: 
java.lang.ClassCastException: android.widget.FrameLayout cannot be cast to 
androidx.drawerlayout.widget.DrawerLayout
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
    at 
android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    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)
    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: java.lang.ClassCastException: android.widget.FrameLayout cannot be cast to 
androidx.drawerlayout.widget.DrawerLayout
    at com.example.pokedex.ui.main.MainActivity.configDrawer(MainActivity.kt:34)
    at com.example.pokedex.ui.main.MainActivity.onCreate(MainActivity.kt:29)
    at android.app.Activity.performCreate(Activity.java:8000)
    at android.app.Activity.performCreate(Activity.java:7984)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)

my Main Activity class

class MainActivity : AppCompatActivity() {

private lateinit var toggle: ActionBarDrawerToggle
private val controlador by lazy {
    findNavController(R.id.pokemons_activity_nav_host)
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.main_activity)
    configNavControler()
    configDrawer()

}

private fun configDrawer() {
    toggle = ActionBarDrawerToggle(this, drawerLayout as DrawerLayout?, R.string.open, 
R.string.close)
    (drawerLayout as DrawerLayout?)?.addDrawerListener(toggle)
    toggle.syncState()

    supportActionBar?.setDisplayHomeAsUpEnabled(true)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    if(toggle.onOptionsItemSelected(item)){
        return true
    }
    return super.onOptionsItemSelected(item)
}


private fun configNavControler() {
    val navView: BottomNavigationView = findViewById(R.id.main_activity_bottom_navigation)
    val navHostFragment = supportFragmentManager
        .findFragmentById(R.id.pokemons_activity_nav_host) as NavHostFragment
    val navController = navHostFragment.navController

    navView.setupWithNavController(navController)
    // configura para ver se o Nav View aparece ou nao
    configNavView(navController, navView)
}

private fun configNavView(
    navController: NavController,
    navView: BottomNavigationView
) {
    // pega o id do fragment e compara,se for igual, faz as atribuições necessárias (aparecer ou 
desaparecer Nav View)
    navController.addOnDestinationChangedListener { controller, destination, arguments ->
        if (destination.id == R.id.entrada_graph) {
            navView.visibility = View.GONE
        } else {
            navView.visibility = View.VISIBLE
          }
       }
    }
}

activity_main xml

<androidx.drawerlayout.widget.DrawerLayout 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/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.main.MainActivity">

<androidx.fragment.app.FragmentContainerView
    android:id="@+id/pokemons_activity_nav_host"
    class="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:defaultNavHost="true"
    app:navGraph="@navigation/nav_graph" />

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/main_activity_bottom_navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="@color/bottomNavigation"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/botao_nav_menu" />


</androidx.constraintlayout.widget.ConstraintLayout>

<com.google.android.material.navigation.NavigationView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:id="@+id/navView"
    app:headerLayout="@layout/nav_header"
    app:menu="@menu/nav_drawer_menu"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"/>

</androidx.drawerlayout.widget.DrawerLayout>

1 answer

0


Error discovered, was with Framelayout declared still in main_activity v21 (I don’t know why I created this class)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.