the android app crashes on launch when I use androidx.fragment.app.FragmentContainerView

112 Views Asked by At

I'm just a beginner to Android development, I'm reading this book "Head first Android development". I'm supposed to make a secret message app in the chapter 6 of this book and I did exactly as the book say. I compared my code with the code in the book and made sure they are the same but the app crashes on launch when I try to launch it on a android device emulator in android studio. There is error message that says "the secret message app keeps stopping" I wonder if you guys can help me with figuring out the problem.

here is my code:

MainActivity.kt

package com.hfad.secretmessage

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_activity)
    }
}

main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.fragment.app.FragmentContainerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_container_view_tag"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:name="com.hfad.secretmessage.WelcomeFragment"
    tools:layout="@layout/fragment_welcome"/>

WelcomeFragment.kt

package com.hfad.secretmessage


import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

class WelcomeFragment : Fragment(){
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_welcome, container, false)
    }
}

fragment_welcome.xml

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginTop="20dp"
        android:textSize="20sp"
        android:text="@string/welcome_text"/>

    <Button
        android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:text="@string/start"/>

</LinearLayout>

strings.xml

<resources>
    <string name="app_name">Secret Message</string>
    <string name="welcome_text">Welcome to the Secret Message app!
        Use this app to encrypt a secret message.
        Click on the Start button to begin.</string>
    <string name="start">Start</string>
    <string name="message_hint">Please enter your secret message</string>
    <string name="next">Next</string>
    <string name="encrypt_text">Here is your encrypted message:</string>
    <!-- TODO: Remove or change this placeholder text -->
    <string name="hello_blank_fragment">Hello blank fragment</string>

</resources>

I tried to use the androidx.appcompat.app.AppCompatActivity class instead of androidx.activity.ComponentActivity and also I added the code super.onCreateView(inflater, container, savedInstanceState) to the WelcomeFragment.kt but the result was the same.

1

There are 1 best solutions below

0
Abdelrahman Mahmoud Nasr On

You have two options:

First(Recommended): to create a navigation file and attach it to the fragmentContainer

Steps

Set Up the Navigation Graph

  1. Add a navigation graph file (File > New > Android Resource File) and filling the fields as follows. It will be like this
  2. File name: nav_graph.xml. This is the same as the name you set for the app:navGraph attribute.
  3. Resource type: Navigation. The Directory name should then automatically change to navigation. A new resource folder called "navigation" will be created.

Upon creating the XML file, you're presented with a new visual editor.To add a new destination, click the new button in the top left of the screen and create a destination for each fragment (one for fragment_letter_list and one for fragment_word_list). like this

Reference the nav_graph in your FragmentContainer

Add this line to your FragmentContainer

app:navGraph="@navigation/nav_graph"

Add some code to initialize the navController

private lateinit var navController: NavController

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_activity)

val navHostFragment = supportFragmentManager
    .findFragmentById(R.id.nav_host_fragment) as NavHostFragment
navController = navHostFragment.navController
    }

You can find more help here : https://developer.android.com/codelabs/basic-android-kotlin-training-fragments-navigation-component#7 but this is a bit advanced I told you the basic steps.

Second option Just add this code in the onCreate fun

val fragment = WelcomeFragment()
supportFragmentManager.beginTransaction()
    .replace(R.id.fragment_container, fragment)
    .commit()

This code will simply display the fragment in the fragment container!

Hope this help, Let me know.