android – "IllegalStateException" error when using a fragment

Question:

Hello everyone, I was tormented with Fragment android. The problem is this. I am getting an exception like this:

 Caused by: java.lang.IllegalStateException: Fragment com.example.kocmuk.swiftkeepass_ultimate2.Activities.EmptyFragment did not create a view.
        at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3748)
        at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:114)
        at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:374)
        at android.support.v4.app.BaseFragmentActivityApi14.onCreateView(BaseFragmentActivityApi14.java:39)
        at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:68)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:777)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
        at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:412)
        at android.app.Activity.setContentView(Activity.java:2457)
        at com.example.kocmuk.swiftkeepass_ultimate2.ListScreen.ListFragment.onCreate(ListFragment.kt:58)
        at android.app.Activity.performCreate(Activity.java:6758)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:159)
        at android.app.ActivityThread.main(ActivityThread.java:6139)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

It drops out when I restart the activity, I did not find a good answer on the Internet. A similar situation is with this question . However, the correct answer has never been given.

Here is my activity (I will cut out some of the code, since there is a lot of code there):

class ListFragment : FragmentActivity(),ListInterface.ViewInterFace {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.list_fragment_activity)
        drawFragment(pathItem)

    }
    override fun drawFragment(pathItem:String) {
        val initialFragment: Fragment = ListScreenFragment()
        val fragmentTransaction = supportFragmentManager.beginTransaction()
        val args = Bundle()
        args.putString("pathGroup", pathItem)
        args.putString("path",pathDb)
        args.putString("password",passwordDb)
        initialFragment.arguments = args
        fragmentTransaction.addToBackStack(null)

        fragmentTransaction?.replace(R.id.content_fragment, initialFragment)
        fragmentTransaction?.commit()
    }
}

Here is the Fragment Activity (Also shortened):

class ListScreenFragment : android.support.v4.app.Fragment() {
    var pathDb = ""
    var passwordDb = ""
    var pathItem = ""
    var breadCrumpsArray: LinkedList<GroupItem>? = null
    var groupItem: GroupItem? = null
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.list_activity, container, false)
        return view
    }
}

And finally, the fragment itself:

<fragment
    android:id="@+id/content_fragment"    
    android:name="com.example.kocmuk.swiftkeepass_ultimate2.Activities.EmptyFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

Question: how to fix this error?

Answer:

It seems to me that the problem is in the way you are calling the fragment. call addToBackStack before replacing the fragment itself.

fun replaceFragment(viewId: Int, fragment: Fragment){
    supportFragmentManager.beginTransaction()
            .replace(viewId, fragment)
            .addToBackStack(null)
            .commit()
}

Try this order. And one more note on the code: the name of the activity should be: MainActivity, BooksListActivity, etc. (ListFragment is a bad name).

Scroll to Top
AllEscort