如何在Kotlin中使用cursorloader

2020年7月16日 4点热度 0条评论

我尝试将Java项目转换为kotlin,但是一个问题使我感到困惑。
项目结构:
App:用于声明全局变量的应用程序MainActivity:AppCompatActivityProvider:ContentProviderSectionsPagerAdapter:FragmentPagerAdapterfragment0:fragment1:片段实现LoaderManager.LoaderCallbacks

当我尝试将fragment1转换为kotlin时,它将抛出FC。
fragment1 java代码:

class fragment1 : Fragment(), LoaderManager.LoaderCallbacks<Cursor> {
private var mMainView: View? = null
private var adapter: SimpleCursorAdapter? = null
private var cursor: Cursor? = null
private var date: Date? = null
private var kadaihao: Array<String?>? = null
private val uri: Uri? = null

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    val inflater = activity.layoutInflater
    mMainView = inflater.inflate(R.layout.fragment1, activity.findViewById(R.id.container) as ViewGroup, false)
}

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

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

    val lv = mMainView!!.findViewById(R.id.lv1) as ListView
    val uiBindFrom = arrayOf("_id", "shuakae", "kadaihao", "shijian", "feilv", "beizhu")
    val uiBindTo = intArrayOf(R.id.ID1, R.id.tv_shuakae1, R.id.tv_kadaihao1, R.id.tv_shijian1, R.id.tv_feilv1, R.id.tv_beizhu1)
    loaderManager.initLoader(1, null, this)
    adapter = SimpleCursorAdapter(
            context, R.layout.item_zhangdan, null, uiBindFrom, uiBindTo,
            CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER)
    lv.adapter = adapter
}

override fun onCreateLoader(id: Int, args: Bundle): Loader<Cursor> {
    val projection = arrayOf("_id", "shuakae", "kadaihao", "shijian", "feilv", "beizhu")
    val selection = "yihuan=0"
    return CursorLoader(context, App.Uri_ZhangDan, projection, selection, null, "kadaihao asc,shijian desc")
}

override fun onLoadFinished(loader: Loader<Cursor>, data: Cursor) {
    adapter!!.swapCursor(data)
}

override fun onLoaderReset(loader: Loader<Cursor>) {
    adapter!!.swapCursor(null)
}
}

尝试运行它并抛出FC:

java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter args
at com.codeme.ccm.fragment1.onCreateLoader(fragment1.kt:0)
at android.support.v4.app.LoaderManagerImpl.createLoader(LoaderManager.java:539)
at android.support.v4.app.LoaderManagerImpl.createAndInstallLoader(LoaderManager.java:548)
at android.support.v4.app.LoaderManagerImpl.initLoader(LoaderManager.java:603)
at com.codeme.ccm.fragment1.onViewCreated(fragment1.kt:73)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1314)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:758)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1984)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:626)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1268)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1116)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1642)
at android.view.View.measure(View.java:18811)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:714)
at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1391)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:784)
at android.view.View.measure(View.java:18811)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:18811)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18811)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18811)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18811)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
at android.view.View.measure(View.java:18811)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2127)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1243)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1479)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1134)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6050)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:860)
at android.view.Choreographer.doCallbacks(Choreographer.java:672)
at android.view.Choreographer.doFrame(Choreographer.java:608)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:846)
at android.os.Handler.handleCallback(Handler.java:742)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:5524)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:740)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:630)

怎么解决呢?

解决方案如下:

我在Android中没有对CursorLoader做很多事情,但是通过堆栈跟踪判断,看来Bundle参数可以为null,但是您将其声明为不可为空,因此Kotlin插入了一些检查来为您验证这一点。

尝试改变

override fun onCreateLoader(id: Int, args: Bundle)


override fun onCreateLoader(id: Int, args: Bundle?)