无法将整数强制转换为android.support.design.widget.FloatingActionButton

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法将整数强制转换为android.support.design.widget.FloatingActionButton相关的知识,希望对你有一定的参考价值。

我试图访问片段内的浮动操作按钮但由于某种原因,我不能。我一直在收到错误。我已经尝试了一切,似乎没有任何工作。这是我片段中的代码:

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>
答案

在调用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
    }

以上是关于无法将整数强制转换为android.support.design.widget.FloatingActionButton的主要内容,如果未能解决你的问题,请参考以下文章

无法将类型“Android.Support.V4.App.Fragment”转换为“Android.Gms.Maps.MapFragment”

android.support.v4.app.FragmentManager无法转换为android.app.FragmentManager

无法从 android.app.FragmentManager 转换为 android.support.v4.app.FragmentManager

android.support.v7.view.menu.ActionMenuItemView 无法转换为 android.widget.Button

我的片段无法转换为 android.support.v4.app.Fragment

比较一个 32 位浮点数和一个 32 位整数而不强制转换为双精度,当任何一个值都可能太大而无法完全适合另一种类型时