2017-09-01 75 views
0

我有這個問題就在這裏:科特林1.1.3-2無法在Android的的onResume實例片段

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與靜態構造類,但我有同樣的結果。

這個問題是隨機的,當我們在另一個應用程序,我們想回到我們的應用程序。

任何幫助?

謝謝。

------編輯------ 這裏是我的科特林對象代碼:

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

看起來像你創建的私有constructor.Please把代碼片段 –

+0

後的代碼片段 –

+0

請出示你的代碼。只有日誌消息是不夠的。 – Joshua

回答

0

當您使用對象關鍵字,使構造私人那麼機器人無法重新創建片段。我的解決方法是:

class MyFragment : Fragment() { 

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

// rest or your code 
}