当前位置: 首页 > 知识库问答 >
问题:

kotlin合成进口不起作用

方永贞
2023-03-14

片段中,针对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函数中设置进度视图的可见性时,我得到的错误是该视图为NULL。

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

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

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

请帮帮我。我是不是漏掉了什么?我对科特林是新来的。

共有1个答案

李胡媚
2023-03-14

您的问题是您试图过早地访问视图。在#oncreateview中,您正在膨胀您的视图,但是fragment#getview()仍然将返回null,只要您不返回膨胀的视图。在内部,Kotlin Android扩展使用getview().findviewbyId(int)方法在您第一次尝试访问视图时绑定视图。

DR

要解决此问题,只需在#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()
        }
    }

}
 类似资料:
  • 是否有类似的删除子记录的问题,但没有使用OlphanRemovation注释? 下面是我在模型类构造函数中的代码示例: cascade=arrayOf(CascadeType.All)的CRUD工作正常,但当将OlphanRemove=true添加到“一对多”注释中时,它无法继续工作,并抛出如下所示的异常: org.hibernate.hibernateException:cascade=“all

  • 当我使用null email向/注册用户发送一个POST请求时,我希望它会说“Hey,you have null in email field”,但由于语句,它会在保存步骤中抛出NPE。当我用错误的电子邮件发送json时,如下所示: 它完美地通过了这个约束注释: 我试过的。我尝试在@validemail、@get:validemail、@field:validemail(kotlin data c

  • 我已经配置了一个路由来从交易所中提取一些数据并聚合它们;这是简单的总结: 问题是聚合完成永远不起作用,例如,这是我的测试示例: ReflelctionTestUtils.setField;ReflectionTestUtils.setFiled;producerTemplate.send(FingerprintHistoryRouteBuilder.FINGERPRINT_HISTORY_ENDP

  • 更新: 如果我使用注释和路径:/*和/api/*-site可以很好地工作。但是因为我使用全局静态IP,所以我不能为每个IP创建一个以上的入口。如果我使用-site返回错误:

  • nav.component.ts app.module.ts app.component.ts

  • 我有两个集合。如果集合2中的1号和2号在集合1中指定的一定范围内,我正在尝试将集合2的文档添加到集合1中。集合1中的FYI ObjectId和集合2中的ObjectId指的是两个不同的项目/产品,因此我无法在此id上加入两个集合。 集合1中的示例文档: 集合2中的示例文档: 我想要输出: 我认为使用管道的查找阶段可以工作。我的代码当前如下: 但是运行上面的没有给我输出。我做错了什么吗??