Kotlin 1.1.3-2无法在Android上实例化片段



我在这里有这个问题:

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) 

我试图使用静态构造函数使用CompanionObject将Kotlin对象更改为类,但我的结果相同。

当我们在另一个应用程序上时,问题是随机解决的,我们想回到我们的应用程序。

有帮助吗?

谢谢。

------编辑-------这是我的kotlin对象代码:

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()
}}

使用对象关键字时,您将构造函数为私有,然后Android无法重新创建您的片段。我的解决方法是:

class MyFragment : Fragment() {
    companion object {
        @JvmStatic val fragment = MyFragment()
    }
// rest or your code 
}

相关内容

  • 没有找到相关文章

最新更新