kotlin中的合成导入不起作用

时间:2018-04-24 06:37:16

标签: android kotlin

Fragment中,由Anko提供的布局的Kotlins合成导入无效。我试图设置视图的可见性,但它说视图为空。我有以下代码。

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

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

This is the null error I am getting

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

    <?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>

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

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

2 个答案:

答案 0 :(得分: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()
        }
    }

}

答案 1 :(得分:0)

请尝试在onViewCreated中使用/执行代码...像这样

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        myRequestListRV.layoutManager = LinearLayoutManager(homeActivity)
    }