1

(I'm a new Android developer so possibly my terminology will be wrong.)

Android Studio 2.3.1 Targeting API 22

I seem to have a program with an error that Instant Run will allow, but that crashes when run from a built apk that is installed from, for instance, email. The situation is this:

I have a button in xml with an onClick property:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:onClick="myOnClick"
/>

And in my Activity, I have code where I (mistakenly) left out the required "public" on myOnClick:

void myOnClick(View v)
{
    Button button = (Button)v;
    button.setText("clicked");
}

(Yes, I do get a warning from Android Studio, saying that myOnClick is never used.)

When I run this from Android Studio using the Run or Debug buttons, the program works "correctly", in that myOnClick is executed without complaint when I click the button. But it shouldn't work, because the required signature is "public void myOnClick(View v)".

Furthermore, if I do "Build APK" and then explicitly install that apk as a file, such as by mailing it to myself on the phone, the app does crash, which I believe is the correct behavior:

Could not find method myOnClick(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton
[trace snipped]

Adding the missing "public" allows the app to run when installed like this from an apk. I would consider this a bug in Android Studio's Instant Run feature, and intend to file it as such unless there's a reason that it's not a bug.

Is there a reason for this, or a bug?

Don Porges
  • 11
  • 2

0 Answers0