【发布时间】:2017-06-17 18:16:24
【问题描述】:
所以我在片段中有以下 ImageButton:
<ImageButton
android:id="@+id/moneyBtn"
style="@android:style/Widget.Holo.Light.ImageButton"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:src="@drawable/monkey"
android:background="@null"/>
还有下面的fragmentActivity.kt
class Home : Fragment() {
override public fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view: View? = inflater.inflate(R.layout.fragment_home, container, false)
val moneyButton: ImageButton = view?.findViewById(R.id.moneyBtn) as ImageButton
val result = MyAppApplication()
var money = result.money
moneyButton.setOnClickListener(View.OnClickListener {
Toast.makeText(activity, "TESTING BUTTON CLICK 1", Toast.LENGTH_SHORT).show()
})
return view
}
我也尝试使用“普通”的 Kotline setOnClickListener
moneyButton.setOnClickListener {
Toast.makeText(activity, "TESTING BUTTON CLICK 1", Toast.LENGTH_SHORT).show()
}
应用程序崩溃和冻结,它只是不工作
我也尝试用 throw 替换 Toast,但这也不会被执行。 也许你能找到我的错误?
【问题讨论】:
-
您介意发布打开片段的 Activity 类吗?
-
我没有任何理由认为该方法在该 UI 的其他线程中运行,但我找不到任何理由。尝试使用
runOnUiThread在 UI 线程中运行 Toast
标签: android kotlin imagebutton