Kotlin 1.1.3-2 Unable to instantiate fragment in OnResume Android

1.8k views Asked by At

I have this problem here:

FATAL EXCEPTION: main Process: com.mytest.app, PID: 20586
                                               java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mytest.app/com.mytest.app.activities.home.HomeActivity}: android.app.Fragment$InstantiationException: Unable to instantiate fragment 
                                               com.mytest.app.activities.fragments.MyFragment: make sure class name exists, is public, and has an empty constructor that is public
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2371)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2423)
                                                   at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
                                                   at android.os.Handler.dispatchMessage(Handler.java:110)
                                                   at android.os.Looper.loop(Looper.java:193)
                                                   at android.app.ActivityThread.main(ActivityThread.java:5327)
                                                   at java.lang.reflect.Method.invokeNative(Native Method)
                                                   at java.lang.reflect.Method.invoke(Method.java:515)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
                                                   at dalvik.system.NativeStart.main(Native Method)
                                                Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.mytest.app.activities.fragments.MyFragment: make sure class name exists, is public, and has an empty constructor that is public
                                                   at android.app.Fragment.instantiate(Fragment.java:605)
                                                   at android.app.FragmentState.instantiate(Fragment.java:98)
                                                   at android.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1759)
                                                   at android.app.Activity.onCreate(Activity.java:903)
                                                   at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:277)
                                                   at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:84)
                                                   at com.mytest.app.activities.MyActivity.onCreate(MyActivity.java:26)
                                                   at com.mytest.app.activities.home.HomeActivity.onCreate(HomeActivity.kt:83)
                                                   at android.app.Activity.performCreate(Activity.java:5343)
                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2423) 
                                                   at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342) 
                                                   at android.os.Handler.dispatchMessage(Handler.java:110) 
                                                   at android.os.Looper.loop(Looper.java:193) 
                                                   at android.app.ActivityThread.main(ActivityThread.java:5327) 
                                                   at java.lang.reflect.Method.invokeNative(Native Method) 
                                                   at java.lang.reflect.Method.invoke(Method.java:515) 
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824) 
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640) 
                                                   at dalvik.system.NativeStart.main(Native Method) 
                                                Caused by: java.lang.IllegalAccessException: access to constructor not allowed
                                                   at java.lang.Class.newInstanceImpl(Native Method)
                                                   at java.lang.Class.newInstance(Class.java:1215)
                                                   at android.app.Fragment.instantiate(Fragment.java:590)
                                                   at android.app.FragmentState.instantiate(Fragment.java:98) 
                                                   at android.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1759) 

I tried to change my Kotlin Object to class using CompanionObject with a static constructor but i have the same result.

The problem is randomized when we are on another application and we want to come back to our app.

Any help?

Thanks.

------EDIT------ Here is my kotlin Object code:

object MyFragment : Fragment() {


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

}

override fun onResume() {
    super.onResume()

}

override fun onDestroy() {
    super.onDestroy()

}

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

override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

}

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {

    inflater.inflate(R.menu.mymenu, menu)

}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    // handle item selection
    when (item.itemId) {
        R.id.action_my_results -> {
            return true
        }
        else -> return super.onOptionsItemSelected(item)
    }
}

override fun onAttach(context: Context) {
    super.onAttach(context)
}

override fun onDetach() {
    super.onDetach()
}}
1

There are 1 answers

0
chandra On BEST ANSWER

When you are using object keyword, you make constructor to private then android cannot recreate your fragment. My workaround is :

class MyFragment : Fragment() {

    companion object {
        @JvmStatic val fragment = MyFragment()
    }

// rest or your code 
}