简体   繁体   中英

android apk imagebutton click crashes

I have been testing my app on my actual device and everything works perfect when I do a debug install. But when I build the APK and install it, the app loads up fine to the main screen. When I click one of the imagebuttons on the main screen, it crashes. Again, only the apk build crashes upon imagebutton click, if I deploy the app to the device through debug or use the emulator, the app works perfect.

In my xml, here is an example of one of the buttons:

<ImageButton 
android:id="@+id/btnHomeServices"
android:src="@drawable/button_homeservices"
android:scaleType="fitCenter"    
android:layout_width="wrap_content"
android:layout_height="wrap_content"                 
android:adjustViewBounds="true" 
android:layout_marginRight="3dip"
android:layout_marginLeft="3dip"
android:background="@null"
android:onClick="HomeServicesClick"/>

And the click event method in code:

public void HomeServicesClick(View view)
{
    //we do some stuff here
}

I even commented out the code inside HomeServicesClick and it still crashes, so I know the problem is not the code its trying to execute in that method.

03-19 11:02:18.605: ERROR/AndroidRuntime(18211): FATAL EXCEPTION: main
03-19 11:02:18.605: ERROR/AndroidRuntime(18211): java.lang.IllegalStateException: Could not find a method HomeServicesClick(View) in the activity class com.WhiteLabel.LoadingScreen for onClick handler on view class android.widget.ImageButton with id 'btnHomeServices'
03-19 11:02:18.605: ERROR/AndroidRuntime(18211):     at android.view.View$1.onClick(View.java:2178)
03-19 11:02:18.605: ERROR/AndroidRuntime(18211):     at android.view.View.performClick(View.java:2532)
03-19 11:02:18.605: ERROR/AndroidRuntime(18211):     at android.view.View$PerformClick.run(View.java:9293)
03-19 11:02:18.605: ERROR/AndroidRuntime(18211):     at android.os.Handler.handleCallback (Handler.java:587)
03-19 11:02:18.605: ERROR/AndroidRuntime(18211):     at android.os.Handler.dispatchMessage(Handler.java:92)

the answer is in the stack trace. from the command line, do "adb logcat" and then force the app to error. you will see a stack trace in the log that tells you exactly (well almost) what the error is.

take a look at,

http://developer.android.com/reference/android/view/View.html#attr_android:onClick

it says,

Name of the method in this View's context to invoke when the view is clicked. This name must correspond to a public method that takes exactly one parameter of type View. For instance, if you specify android:onClick="sayHello", you must declare a public void sayHello(View v) method of your context (typically, your Activity).

The problem is that you shouldn't specify onClick in your layout file if you are going to make a release build / use proguard. You should use setOnClickListener in the onCreate instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM