kotlin.TypeCastException:无法将null强制转换为非null类型android.support.design.widget.FloatingActionButton

时间:2018-06-26 11:33:34

标签: android kotlin

我知道这个问题已经问了很多遍了,但是我真的不明白为什么我不能在TabLayout的一个片段中添加“浮动操作按钮”。

它给我的错误是“ kotlin.TypeCastException:无法在我的logcat中将null强制转换为非null类型的android.support.design.widget.FloatingActionButton”。

我是Android和应用程序开发的新手,非常感谢这里的帮助,因为我已经浪费了3天的时间

MainActivity代码:

class MainActivity : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val fab = findViewById<View>(R.id.fab) as FloatingActionButton

    fab.setOnClickListener { view ->
        Snackbar.make(view, "Replace with your own action",
                Snackbar.LENGTH_LONG)
                .setAction("Action", null).show()
    }
    initToolbar()


    val tabLayout: TabLayout = findViewById(R.id.tab_layout)

    val viewPager: ViewPager = findViewById(R.id.view_pager)

    val adapter = SampleAdapter(supportFragmentManager)


    viewPager.adapter = adapter



    tabLayout.setupWithViewPager(viewPager)

    tabLayout.addOnTabSelectedListener(object : 
 TabLayout.OnTabSelectedListener {
        override fun onTabSelected(tab: TabLayout.Tab) {

        }

        override fun onTabUnselected(tab: TabLayout.Tab) {

        }

        override fun onTabReselected(tab: TabLayout.Tab) {

        }
    })


    }

  private fun initToolbar() {
    val toolbar: Toolbar = findViewById(R.id.toolbar)
    setSupportActionBar(toolbar)
    supportActionBar!!.title = "Don't Forget"
  }
  }

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:contentInsetStartWithNavigation="0dp"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        style="@style/CustomTabLayout"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_gravity="left"
        android:background="@color/colorPrimary"
        app:tabGravity="fill"
        app:tabMode="fixed"
        app:tabTextAppearance="@style/TextAppearance.AppCompat" />

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

<android.support.v4.view.ViewPager
    android:id="@+id/view_pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

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

logcat:

  java.lang.RuntimeException: Unable to start activity 
  ComponentInfo{com.ashwinis.mylistapp/com.ashwinis.mylistapp.MainActivity}: 
  kotlin.TypeCastException: null cannot be cast to non-null type 
  android.support.design.widget.FloatingActionButton
 at 
 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
 at android.app.ActivityThread.-wrap11(Unknown Source:0)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
 at android.os.Handler.dispatchMessage(Handler.java:105)
 at android.os.Looper.loop(Looper.java:164)
 at android.app.ActivityThread.main(ActivityThread.java:6541)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
 Caused by: kotlin.TypeCastException: null cannot be cast to non-null type 
 android.support.design.widget.FloatingActionButton
 at com.ashwinis.mylistapp.MainActivity.onCreate(MainActivity.kt:25)
 at android.app.Activity.performCreate(Activity.java:6975)
 at 
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
at android.os.Handler.dispatchMessage(Handler.java:105) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6541) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)  

fragment_one.xml

<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
    android:layout_width="match_parent"
    android:layout_height="500dp"
    android:gravity="center_vertical|center_horizontal"
    android:text="FragmentOne" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_marginBottom="16dp"
    android:layout_marginEnd="16dp"
    app:backgroundTint="@color/colorAccent"
    app:fabSize="mini"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:srcCompat="@android:drawable/ic_input_add" />

1 个答案:

答案 0 :(得分:4)

您需要在XML中添加ID为FloatingActionButton的{​​{1}},这将使其为空。

此外,在将可能为null的内容转换为非null类型(此处为FloatingActionButton)时,您需要使用fab

尝试

?

有关更多信息,请阅读https://kotlinlang.org/docs/reference/typecasts.html#safe-nullable-cast-operator

编辑

@Pawel的评论非常有帮助。阅读https://kotlinlang.org/docs/tutorials/android-plugin.html摆脱val fab = findViewById<View>(R.id.fab) as? FloatingActionButton 的通话,这是NPE的主要原因。