kotlin中的合成导入不起作用

nic*_*tdr 3 android kotlin

在一个Fragment,Kotlins合成导入提供的布局Anko是行不通的.我试图设置视图的可见性,但它说视图为空.我有以下代码.

class HistoryFragment : Fragment(), HistoryContract.view, LoaderManager.LoaderCallbacks<List<SongEntity>>{

    private lateinit var mPresenter : HistoryContract.Presenter
    private lateinit var adapter: HistoryAdapte
    private val loaderId : Int = 101

    override fun onLoadFinished(loader: Loader<List<SongEntity>>?, data: List<SongEntity>?) {
        hideLoadingIndicator()

        if (data == null) {
            showErrorView()
        } else {
            //update the adapter
            adapter.updateDataset(data)
        }
    }

    override fun onLoaderReset(loader: Loader<List<SongEntity>>?) {
    }

    override fun onCreateLoader(id: Int, args: Bundle?): Loader<List<SongEntity>> {
       showLoadingIndicator()

       return object : AsyncTaskLoader<List<SongEntity>>(activity!!.baseContext){
            override fun loadInBackground(): List<SongEntity>? {
                return mPresenter.fetchSongs()
            }
        }
    }

    override fun showLoadingIndicator() {
        song_history_pv.visibility = View.VISIBLE
    }

    override fun hideLoadingIndicator() {
        song_history_pv.visibility = View.INVISIBLE
    }

    override fun showErrorView() {
        song_history_error_tv.visibility = View.VISIBLE
    }

    override fun hideErrorView() {
        song_history_error_tv.visibility = View.INVISIBLE
    }

    override fun setPresenter(presenter: HistoryContract.Presenter) {
        mPresenter = presenter
    }

    override fun showDiscoveredSongs(songs: List<SongEntity>) {
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        val rootView = inflater.inflate(R.layout.fragment_song_history, container, false)
        loaderManager.initLoader(loaderId, null , this)
        songHistoryRv.layoutManager = LinearLayoutManager(context)
        songHistoryRv.adapter = HistoryAdapter {
            Toast.makeText(activity!!.baseContext, "${it.name}", Toast.LENGTH_SHORT).show()
        }

        return rootView
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,当我在showLoadingIndicator函数中设置进度视图的可见性时,我收到视图为空的错误.

这是我得到的空错误

下面是我用于片段的布局文件

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/songHistoryRv"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <ProgressBar
        android:id="@+id/song_history_pv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/song_history_error_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/song_history_error"
        android:visibility="invisible"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

import kotlinx.android.synthetic.main.fragment_song_history.*是我正在使用的综合导入.

请帮我解决这个问题.我错过了什么吗?我是kotlin的新手.

reV*_*rse 5

您的问题是您过早地尝试访问该视图.在#onCreateView你膨胀你的视图但是Fragment#getView()只要你没有返回膨胀的视图,它仍然会返回null.在内部,Kotlin Android Extensions使用getView().findViewById(int)方法在您第一次尝试访问视图时绑定视图.

TL;博士

要解决您的问题,只需返回膨胀的视图,#onCreateView然后将您的操作LoaderManager移至#onViewCreated:

class HistoryFragment : Fragment(), HistoryContract.view, LoaderManager.LoaderCallbacks<List<SongEntity>>{

    // ...

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

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        loaderManager.initLoader(loaderId, null , this)
        songHistoryRv.layoutManager = LinearLayoutManager(context)
        songHistoryRv.adapter = HistoryAdapter {
            Toast.makeText(activity!!.baseContext, "${it.name}", Toast.LENGTH_SHORT).show()
        }
    }

}
Run Code Online (Sandbox Code Playgroud)