图片按钮操作无效或崩溃应用

时间:2017-12-10 19:32:03

标签: android alertdialog imagebutton

我编写了一个简单的按钮动作。它工作正常。 现在,我试图为图像按钮编写相同的动作。 我遵循相同的想法,但应用程序不会将图像识别为按钮,因为当我点击它时,没有任何反应。 编辑:我现在正试图使用​​android:onClick。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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">

    <ImageButton
        android:id="@+id/btnBoxie"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginBottom="352dp"
        android:layout_marginTop="8dp"
        android:background="@null"
        android:scaleType="fitCenter"
        android:clickable="true"
        android:onClick="passAction"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0"
        app:srcCompat="@drawable/boxie_icon" />
    </android.support.constraint.ConstraintLayout>

我的目的是显示一个简单的对话框。

public class OffActivity extends AppCompatActivity
{
@Override
protected void onCreate( Bundle savedInstanceState ) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_off );

    View btnBoxie = findViewById( R.id.btnBoxie );

    passAction( btnBoxie ); //New trial

    //Doesn't work
    /*btnBoxie.setOnClickListener( new ImageButton.OnClickListener()
    {
        public void onClick( View v )
        {
            AlertDialog alertDialog = new AlertDialog.Builder( OffActivity.this ).create();
            alertDialog.setTitle( "Dialog Boxie" );
            alertDialog.setMessage( "Hi, you've just clicked on me. Don't bother me again :)" );
            alertDialog.show();
        }
    });*/
}

public void passAction( View w )
{
    //Button btn = (Button) w;

    //String msg = "Can you see Boxie?";
    //msg = msg.concat( btn.getText().toString() );

    AlertDialog alertDialog = new AlertDialog.Builder( OffActivity.this).create();
    alertDialog.setTitle( "You have been trapped by mistake!" );
    alertDialog.setMessage( "Boxie used to be a button, but some invisible mistake is cutting her heart." );
    alertDialog.show();
}

}

logcat的:

FATAL EXCEPTION: main
    Process: com.example.joaoliveira.firstoftheyear, PID: 22152
    java.lang.IllegalStateException: Could not find method passAction(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'btnBoxie'
        at android.view.View$DeclaredOnClickListener.resolveMethod(View.java:4482)
        at android.view.View$DeclaredOnClickListener.onClick(View.java:4446)
        at android.view.View.performClick(View.java:5201)
        at android.view.View$PerformClick.run(View.java:21163)
        at android.os.Handler.handleCallback(Handler.java:746)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5443)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

我已尝试按<ImageButton更改android.support.v7.widget.AppCompatButton,这样会隐藏图片,应用仍会崩溃。

1 个答案:

答案 0 :(得分:0)

试试这个:

<ImageButton
    android:id="@+id/btnBoxie"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_marginBottom="352dp"
    android:layout_marginTop="8dp"
    android:background="@null"
    android:contentDescription="@string/Boxie"
    android:scaleType="fitCenter"
    android:clickable="true"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="1.0"
    app:srcCompat="@drawable/box_icon" />

在xml中添加此按钮是可单击的。