Android seflmake工具栏中带有onClick的ImageButton
您好:)我想向工具栏中的ImageButton添加onClick方法 它使用Android seflmake工具栏中带有onClick的ImageButton,android,onclick,crash,android-toolbar,android-imagebutton,Android,Onclick,Crash,Android Toolbar,Android Imagebutton,您好:)我想向工具栏中的ImageButton添加onClick方法 它使用onBackPressed()工作,但不是通过单击ImageButton。应用程序正在崩溃,但我无法从控制台读取错误消息,我还不知道如何正确调试它 你能帮我找出可能丢失的东西吗? 非常感谢:) toolbar\u room.xml <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
onBackPressed()
工作,但不是通过单击ImageButton。应用程序正在崩溃,但我无法从控制台读取错误消息,我还不知道如何正确调试它
你能帮我找出可能丢失的东西吗?
非常感谢:)
toolbar\u room.xml
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar_room"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:layout_width="?attr/actionBarSize"
android:layout_height="match_parent"
android:background="@drawable/ic_back"
android:id="@+id/btn_roomBack"
android:onClick="backToMain"/>
</LinearLayout>
<android.support.v7.widget.LinearLayoutCompat 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.hftl.smartcast.RoomActivity"
android:orientation="vertical">
<include layout="@layout/toolbar_room"></include>
//Code
注意:我添加了android:parentActivityName=“.RoomActivity”
到my AndroidManifest.xml。您应该像下面这样将视图参数传递给函数,以便在单击xml时使用
public void backToMain(View v){
非常感谢。然后我必须将v提供给
AlertDialog.Builder alertDlg=new AlertDialog.Builder(v)代码>?您不需要这样做。因为“构建器(this)”中的“this”用于上下文。
public void backToMain(View v){