如何使用Kotlin修复此错误,找不到Fragment构造函数?

时间:2018-09-11 04:49:07

标签: android android-fragments kotlin

我正在使用Kotlin开发Android应用程序。在我的应用程序包含TabViewPager的应用程序中,因此我实现了两个标签。应用停止运行,并且logcat显示在错误下方。

  

java.lang.RuntimeException:无法启动活动ComponentInfo {com.crypto.wallet / com.crypto.wallet.activities.MainActivity}:android.support.v4.app.Fragment $ InstantiationException:无法实例化片段com。 crypto.wallet.activities.ReceiveFragment:找不到Fragment构造子

MyFragment.kt

@SuppressLint("ValidFragment")
class ReceiveFragment(private val transactionList: List<TransactionEntity>, val appDatabase: AppDatabase, private val direction: TransactionAdapterDirection,
                      val networkDefinitionProvider: NetworkDefinitionProvider) : Fragment(){

    private var linearLayoutManager: LinearLayoutManager? = null

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

    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

        val rootView = inflater.inflate(R.layout.receive_fragment, container, false)
        val recyclerView = rootView.findViewById<RecyclerView>(R.id.transaction_recycler_in) as RecyclerView
        linearLayoutManager = LinearLayoutManager(getActivity(), LinearLayout.VERTICAL, false)
        recyclerView.layoutManager = linearLayoutManager
        recyclerView.adapter = TransactionRecyclerAdapter(transactionList,appDatabase,direction,networkDefinitionProvider)
        recyclerView.setHasFixedSize(true);
        return rootView
    }
}

ViewPager.kt

override fun getItem(position: Int): Fragment? {
        var fragment: Fragment? = null
        when (position) {
            //0 -> fragment = ReceiveFragment(MytransactionList,MyappDatabase,Myincoming,MynetworkDefinitionProvider)
            0 -> fragment = ReceiveFragment(MyIT,MyAppDatabase,MyIncoming,MyNetwork)
            1 -> fragment = SendFragment()
        }

        return fragment
    }

Main.kt OnCreate()

val viewPager = findViewById<ViewPager>(R.id.viewpager)
                if (viewPager != null) {
                    val adapter = ViewPagerAdapter(supportFragmentManager,it,appDatabase,INCOMING,networkDefinitionProvider)
                    viewPager.adapter = adapter
                }

 val pref = applicationContext.getSharedPreferences("MyPref", 0) // 0 - for private mode
                val editor = pref.edit()
                val gson = Gson()
                val json = gson.toJson(it)
                editor.putString("MyObject", json)
                editor.apply()

OnResume()

val prefs = applicationContext.getSharedPreferences("MyPref", 0)
        val gson = Gson()
        val json = prefs.getString("MyObject", "")
        val person1: List<TransactionEntity> = gson.fromJson(json, ArrayList<TransactionEntity>()::class.java)

更新: 在我尝试了这个之后,我得到了同样的错误(我尝试了Bundle和Shared pref):

companion object {
        @JvmStatic
        fun newInstance(myIT: List<TransactionEntity>, myAppDatabase: AppDatabase, myIncoming: TransactionAdapterDirection,
                        myNetwork: NetworkDefinitionProvider,
                        bundle: Bundle) =
                ReceiveFragment(myIT, myAppDatabase, myIncoming, myNetwork,bundle).apply {
                    arguments = Bundle().apply {
                       /* val prefs = getActivity()!!.getApplicationContext().getSharedPreferences("myPrefs", 0)
                        val gson = Gson()
                        val json = prefs.getString("MyObject", "")
                        val person1: List<TransactionEntity> = gson.fromJson(json,
                                ArrayList<TransactionEntity>()::class.java)
                        Log.d("Karthi", "Frag-GSON " + person1)*/

                        val dd = bundle.getSerializable("MySerializable")
                        Log.d("Karthi", "Frag-GSON " + dd)
                    }
                }
    }

Main.kt:

val bundle = Bundle()
                val gson_budle = Gson()
                val json_bundle = gson_budle.toJson(it)
                bundle.putSerializable("MySerializable", json_bundle)

                val sharedPreferences = getSharedPreferences(myPreferences, Context.MODE_PRIVATE)
                val editor = sharedPreferences.edit()
                val gson = Gson()
                val json = gson.toJson(it)
                editor.putString("MyObject", json)
                editor.putString("MyObject_json_bundle", json_bundle)
                Log.d("Karthi","After - IT" + json)
                Log.d("Karthi","After - IT" + json_bundle)
                editor.apply()
  

致命异常:主要                                                                      流程:com.crypto.wallet,PID:29313                                                                      java.lang.RuntimeException:无法启动活动ComponentInfo {com.crypto.wallet / com.crypto.wallet.activities.MainActivity}:android.support.v4.app.Fragment $ InstantiationException:无法实例化片段com.crypto.wallet .activities.ReceiveFragment:找不到Fragment构造函数   在android.support.v4.app.FragmentController.restoreAllState(FragmentController.java:152)                                                                          在android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:330)                                                                          在android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:84)                                                                          在com.crypto.wallet.activities.MainActivity.onCreate(MainActivity.kt:194)

3 个答案:

答案 0 :(得分:0)

当您对错误进行简短描述时,您会看到:

避免在片段中使用非默认构造函数:使用默认构造函数加Fragment setArguments(Bundle)代替。 从Fragment文档中:

每个片段都必须有一个空的构造函数,因此可以在恢复其活动状态时实例化它。强烈建议子类不要使用带有参数的其他构造函数,因为在重新实例化片段时,将不会调用这些构造函数。相反,参数可以由调用者使用setArguments(Bundle)提供,然后由Fragment使用getArguments()检索。

例如:

 fun newInstance(bundle : Bundle) : ReceiveFragment{
        val fragment = ReceiveFragment()
         fragment.arguments=bundle           
        return fragment
    }

现在调用片段:

 0 -> fragment = ReceiveFragment.newInstance(new Bundle)

更多信息:http://developer.android.com/reference/android/app/Fragment.html#Fragment()

答案 1 :(得分:0)

您可以做的是用零个参数创建一个默认构造函数

companion object {
    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment LoginFragment.
     */
    // TODO: Rename and change types and number of parameters
    @JvmStatic
    fun newInstance(param1: String, param2: String) =
            ReceiveFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
// Or from your code, you can use Gson
   val prefs = applicationContext.getSharedPreferences("MyPref", 0)
   val gson = Gson()
   val json = prefs.getString("MyObject", "")
   val person1: List<TransactionEntity> = gson.fromJson(json, 
   ArrayList<TransactionEntity>()::class.java)

            }
}

您可以像使用它

   0 -> receiveFragment= ReceiveFragment.newInstance(firstParam, secondParam)

您可以在onCreate

中收到此参数
 override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    arguments?.let {
        email = it.getString(ARG_PARAM1)
        password = it.getString(ARG_PARAM2)

    }
  // Or from your code, you can use Gson
   val prefs = applicationContext.getSharedPreferences("MyPref", 0)
   val gson = Gson()
   val json = prefs.getString("MyObject", "")
   val person1: List<TransactionEntity> = gson.fromJson(json, 
   ArrayList<TransactionEntity>()::class.java)
}

还有这样的片段构造器

class ReceiveFragment: Fragment()

希望这会有所帮助

答案 2 :(得分:0)

如前所述here

如果无参数构造函数不可用,状态恢复时某些情况下会出现运行时异常。

所以请像这样更改您的代码

class ReceiveFragment : Fragment{

   private val transactionList: List<TransactionEntity>? = null
   val appDatabase: AppDatabase? = null
   private val direction: TransactionAdapterDirection? = null
   val networkDefinitionProvider: NetworkDefinitionProvider? = null

   //Add empty constructor to avoid the exception
   constructor() : super()

   constructor(transactionList: List<TransactionEntity>, appDatabase: AppDatabase, direction: TransactionAdapterDirection, networkDefinitionProvider: NetworkDefinitionProvider){
      this.transactionList = transactionList
      this.appDatabase = appDatabase
      this.direction = direction
      this.networkDefinitionProvider = networkDefinitionProvider
   }
}