片段按钮onClickListener不执行

我有一个片段内的按钮。 moneyBtn获取使用val button:Button? = view?.findViewById(R.id.moneyBtn) as Button? val button:Button? = view?.findViewById(R.id.moneyBtn) as Button? 我用了? 因为IDE会抛出语法错误。

button现在使用其setOnClickListener:

 button?.setOnClickListener { moneyGetten() mEditor.putInt("money", money++) mEditor.commit() moneyGetten() Log.d("BUTTON CLICKED", "!") } 

我的理论:视图不会因为某些原因而被找到,所以findView会以null , 是“保护”的OnClickListener。

这是我的XML:

 <FrameLayout 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" tools:context="de.fosefx.clickergame.Generator"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/plus1label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="135dp" android:text="+1" android:textAppearance="@style/TextAppearance.AppCompat.Display1" /> <Button android:id="@+id/moneyBtn" style="@style/Widget.AppCompat.Button.Colored" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="67dp" android:text="CLICK ME" /> </RelativeLayout> </FrameLayout> 

不知何故,这个观点没有被宣布。 现在工作。 永远不要再犯这个错误。