0

My app crashes without an error message when I try to show a DialogFragment, I have absolutely no Idea why this is happening.

When I say it crashes, I mean that the UI of the app freezes and no action is done anymore.

In my dialog I want to display a ViewPager, which displays two more fragments inside the dialog.

My code:

Dialog:

@AndroidEntryPoint
class SetSiteDialog : DialogFragment() {

    private var _binding: DialogSetSiteBinding? = null
    private val binding get() = _binding!!

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NORMAL, R.style.AppTheme_FullScreenDialog)
    }

    override fun onStart() {
        super.onStart()
        dialog?.window?.setLayout(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        )
    }
    
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = DialogSetSiteBinding.inflate(inflater, container, false)

        binding.viewPager.adapter = PagerAdapter(this)

        TabLayoutMediator(binding.tabLayout, binding.viewPager) { tab, position ->
            tab.text = when (position) {
                PagerAdapter.PAGE_SEARCH -> getString(R.string.dialog_location_specification_option_search)
                PagerAdapter.PAGE_FREE_INPUT -> getString(R.string.dialog_location_specification_option_self)
                else -> ""
            }
        }.attach()

        return binding.root
    }

}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true">

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/toolbar"
            style="@style/Toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:menu="@menu/menu_save"
            app:navigationIcon="@drawable/icon_close"
            app:title="@string/set_site" />

    </com.google.android.material.appbar.AppBarLayout>

    <RelativeLayout
        android:layout_below="@id/appbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/viewPager"
            android:layout_below="@id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </RelativeLayout>

</RelativeLayout>

Fragment1:

package com.mugler.movis.ui.dialogs.setsite.controller

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.mugler.movis.databinding.FragmentSetSiteSearchBinding

class SearchSiteFragment : Fragment() {

    private var _binding: FragmentSetSiteSearchBinding? = null
    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentSetSiteSearchBinding.inflate(inflater, container, false)

        return binding.root
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_centerInParent="true"
        android:text="Search"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

Fragment2:

@AndroidEntryPoint
class FreeInputFragment : Fragment() {

    private var _binding: FragmentSetSiteFreeInputBinding? = null
    private val binding get() = _binding!!
    private val viewModel: SetSiteDialogViewModel by activityViewModels()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentSetSiteFreeInputBinding.inflate(inflater, container, false)
        binding.lifecycleOwner = viewLifecycleOwner
        binding.viewModel = viewModel

        return binding.root
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

}
<?xml version="1.0" encoding="utf-8"?>
<layout>

    <data>

        <variable
            name="viewModel"
            type="com.mugler.movis.ui.dialogs.setsite.viewmodel.SetSiteDialogViewModel" />
    </data>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="@dimen/default_margin">

        <com.google.android.material.textfield.TextInputLayout
            style="@style/DefaultTextField"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/location_number">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:imeOptions="actionNext"
                android:inputType="text"
                android:nextFocusForward="@id/textFieldStreet"
                android:text="@={viewModel.siteNumber}" />

        </com.google.android.material.textfield.TextInputLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/small_margin"
            android:baselineAligned="false"
            android:orientation="horizontal">

            <com.google.android.material.textfield.TextInputLayout
                style="@style/DefaultTextField"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="4"
                android:hint="@string/street">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/textFieldStreet"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:imeOptions="actionNext"
                    android:inputType="textPostalAddress"
                    android:nextFocusForward="@id/textFieldStreetNumber"
                    android:text="@={viewModel.siteStreet}" />

            </com.google.android.material.textfield.TextInputLayout>

            <com.google.android.material.textfield.TextInputLayout
                style="@style/DefaultTextField"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/default_margin"
                android:layout_weight="1"
                android:hint="@string/number_abbreviation">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/textFieldStreetNumber"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:imeOptions="actionNext"
                    android:inputType="textPostalAddress"
                    android:nextFocusForward="@id/textFieldPostalCode"
                    android:text="@={viewModel.siteStreetNumber}" />

            </com.google.android.material.textfield.TextInputLayout>

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/small_margin"
            android:baselineAligned="false"
            android:orientation="horizontal">

            <com.google.android.material.textfield.TextInputLayout
                style="@style/DefaultTextField"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:hint="@string/postal_code_abbreviation">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/textFieldPostalCode"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:imeOptions="actionNext"
                    android:inputType="numberSigned"
                    android:nextFocusForward="@id/textFieldCity"
                    android:text="@={viewModel.sitePostalCode}" />

            </com.google.android.material.textfield.TextInputLayout>

            <com.google.android.material.textfield.TextInputLayout
                style="@style/DefaultTextField"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/default_margin"
                android:layout_weight="2"
                android:hint="@string/city">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/textFieldCity"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:imeOptions="actionDone"
                    android:inputType="textPostalAddress"
                    android:text="@={viewModel.siteCity}" />

            </com.google.android.material.textfield.TextInputLayout>

        </LinearLayout>

    </LinearLayout>
</layout>
ATP
  • 2,939
  • 4
  • 13
  • 34
Daniel
  • 380
  • 2
  • 14
  • Does this answer your question? [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Selvin Jun 02 '22 at 10:26
  • `"My app crashes without an error message"` did you take a look at the logcat? – ATP Jun 02 '22 at 10:29
  • Yes I did, but it doesn't show an error or warning – Daniel Jun 02 '22 at 10:48

1 Answers1

1

If the app crashes and nothing shows up in the logcat of Android Studio, the first please check if you are following the correct device in the logcat:

Logcat device

If you are actually following the correct device, then try to invalidate caches and restart Android Studio. Go to File -> Invalidate Caches... -> Invalidate and Restart.

Then run the program again and check if logcat is now showing the error message.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Daniel Faria
  • 35
  • 1
  • 6