androidbuttoncrash

REVOLVED - Android my App crash after set button disabled


My app crashes after I pressed a button.

My code:

<android.support.v7.widget.AppCompatButton
    android:id="@+id/btn_print_trans"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="40dp"
    android:onClick="OnClickPrintSimpleApiTest"
    android:text="PRINT"
    android:textColor="#FFFFFF" />

and:

public void OnClickPrintSimpleApiTst(View view) {
    final Button BTN_print = (Button) findViewById(R.id.btn_print_trans);
    BTN_print.setBackgroundColor(Color.GREEN);
    BTN_print.setEnabled(false);
}  

UPDATE

I didn't realize that I had made a mistake in writing the on click function

RESOLVED


Solution

  • Because the OP defined the onClick method OnClickPrintSimpleApiTest as an attribute in their xml layout file as:

    android:onClick="OnClickPrintSimpleApiTest"

    They do not need to get a reference to the Button using findViewById().

    The Button view is passed to the OnClickPrintSimpleApiTest() method as the parameter "view". Therefore, simply do this:

    public void OnClickPrintSimpleApiTest(View view) {
        Button BTN_print = (Button) view
        BTN_print.setBackgroundColor(Color.GREEN);
        BTN_print.setEnabled(false);
    }