简体   繁体   English

Android APK ImageButton单击崩溃

[英]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. 但是,当我构建并安装APK时,该应用会很好地加载到主屏幕。 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. 同样,单击imagebutton时,只有apk构建会崩溃,如果我通过调试将应用程序部署到设备上或使用仿真器,则该应用程序将完美运行。

In my xml, here is an example of one of the buttons: 在我的xml中,这是按钮之一的示例:

<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: 代码中的click事件方法:

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. 我什至注释掉了HomeServicesClick内部的代码,它仍然崩溃,所以我知道问题不在于它试图在该方法中执行的代码。

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. 从命令行执行“ adb logcat”,然后强制应用程序出错。 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 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. 单击该视图时在此View上下文中调用的方法的名称。 This name must correspond to a public method that takes exactly one parameter of type View. 该名称必须对应于一个公共方法,该方法只使用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). 例如,如果您指定android:onClick =“ sayHello”,则必须声明上下文(通常是Activity)的公共void sayHello(View v)方法。

The problem is that you shouldn't specify onClick in your layout file if you are going to make a release build / use proguard. 问题是,如果要进行发布构建/使用proguard,则不应在布局文件中指定onClick。 You should use setOnClickListener in the onCreate instead. 您应该在onCreate中使用setOnClickListener。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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