有什么方法可以为Fragment中的Button编写setOnClickListener?

时间:2019-01-27 21:03:50

标签: android kotlin fragment buttonclick

我正在Kotlin的Fragment中为Button写setOnClickListener,但是它不起作用,并且在btnAdd使用行的Fragment中说“ NullPointerException”。

class TODOFragment : Fragment() {
private var works = ArrayList<WorkData>()
override fun onCreateView(inflater: LayoutInflater, container:
ViewGroup?, savedInstanceState: Bundle?): View? {
    val view = inflater.inflate(R.layout.work_list, container, false)
    view.work_list.layoutManager = LinearLayoutManager(context)
    works = Database.database().getWorksByStatus(3)
    val adapter = WorkAdapter(works)
    view.work_list.adapter = adapter

    btnAdd.setOnClickListener {
        val dialog = AddProductDialog(context!!)
        dialog.setOnClickListener { title, description ->
            Database.database().addWork(title, description)
            adapter.data.add(WorkData(title, description, Status.TODO))
        }
        dialog.show()
        adapter.notifyItemInserted(works.size + 1)
        work_list.layoutManager!!.scrollToPosition(adapter.itemCount)
    }
    return view
}
}

这是我的activity_main

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.design.widget.TabLayout
    android:id="@+id/tabLayout"
    android:layout_width="match_parent"
    android:layout_height="?actionBarSize">

</android.support.design.widget.TabLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</android.support.v4.view.ViewPager>

<ImageButton
    android:id="@+id/btnAdd"
    android:layout_width="64dp"
    android:layout_height="64dp"
    android:layout_gravity="bottom|right"
    android:layout_margin="32dp"
    android:background="@drawable/btn_circle_bgn"
    android:src="@drawable/ic_add"
    tools:ignore="RtlHardcoded" />
</FrameLayout>

我不明白我在做什么错

2 个答案:

答案 0 :(得分:1)

您的按钮是展开视图的一部分。

要获取它,您需要在它的视图中使用它,如下所示:

override fun onCreateView(inflater: LayoutInflater, container:
ViewGroup?, savedInstanceState: Bundle?): View? {
    val view = inflater.inflate(R.layout.work_list, container, false)
    view.work_list.layoutManager = LinearLayoutManager(context)
    works = Database.database().getWorksByStatus(3)
    val adapter = WorkAdapter(works)
    view.work_list.adapter = adapter

    view.btnAdd.setOnClickListener {
        val dialog = AddProductDialog(context!!)
        dialog.setOnClickListener { title, description ->
            Database.database().addWork(title, description)
            adapter.data.add(WorkData(title, description, Status.TODO))
        }
        dialog.show()
        adapter.notifyItemInserted(works.size + 1)
        work_list.layoutManager!!.scrollToPosition(adapter.itemCount)
    }
    return view
}

答案 1 :(得分:0)

如果您不使用kotlin android extensions plugin,则需要使用类似的内容来找到按钮。

btnAdd = view.findViewById(R.id.btnAdd) as Button

在调用setOnClickListener方法之前

这同样适用于以XML定义的任何其他视图项。