整数不能转换为android.support.design.widget.FloatingActionButton

时间:2018-08-25 18:18:26

标签: android android-fragments kotlin

我正在尝试访问片段中的“浮动操作按钮”,但是由于某种原因,我无法。我不断出错。我已经尝试了一切,但似乎没有任何效果。这是我片段中的代码:

import android.os.Bundle
import android.support.design.widget.FloatingActionButton
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import com.google.firebase.database.DatabaseReference
import com.google.firebase.database.FirebaseDatabase
import com.ntx_deisgns.cyberchatter.cyberchatter.Message
import com.ntx_deisgns.cyberchatter.cyberchatter.R
import kotlinx.android.synthetic.main.groups.*
import kotlinx.android.synthetic.main.groups2.*

class groups2 : Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
//        inflater.inflate(R.layout.groups2, container, false)
        val fab = R.id.fab as FloatingActionButton

        fab.setOnClickListener {

            if (!mainActivityEditText.text.toString().isEmpty()){
                sendData()
            }else{
//                Toast.makeText(this, "Please enter a message", Toast.LENGTH_SHORT).show()
            }
        }
        return inflater.inflate(R.layout.groups2, container, false)
    }

    private fun sendData(){

        val editText = groupInput
        val database = FirebaseDatabase.getInstance()
        val myRef = database.getReference("message")

        myRef.setValue(Message(editText.text.toString()))
        val mDatabase: DatabaseReference? = null
        mDatabase?.
                child("messages")?.
                child(java.lang.String.valueOf(System.currentTimeMillis()))?.
                setValue(Message(editText.text.toString()))

        //clear the text
        editText.setText("")
    }

    companion object {
        fun newInstance(): groups2 = groups2()
    }
}

我在做什么错?为什么在片段中访问setOnClickListener如此困难?

更新:

这是我的groups2.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/groups2_relative"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="60dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="55dp"
    tools:context="com.ntx_deisgns.cyberchatter.cyberchatter.MainActivity">

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/myFab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:clickable="true"
        android:focusable="true"
        android:src="@drawable/ic_send_black_24dp"
        android:tint="@android:color/white"
        app:fabSize="mini"  />

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_toStartOf="@id/myFab">

        <EditText
            android:id="@+id/groupInput"
            android:layout_width="match_parent"
            android:layout_height="57dp"
            android:hint="Please enter your text here" />

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

    <ListView
        android:id="@+id/list_of_messages"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/myFab"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="16dp"
        android:divider="@android:color/transparent"
        android:dividerHeight="16dp" />
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

您需要先对视图进行充气,然后再对其调用findViewById。通货膨胀之前访问浮动操作按钮会导致空指针异常。

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        //inflate and store view
        val view =  inflater.inflate(R.layout.groups2, container, false)
        val fab = view.findViewById(R.id.fab) as FloatingActionButton
        //If using kotlinx then above line is not needed

        fab.setOnClickListener {
                   //do your thing...
           }
        return view
    }