起因
在写一个控件的时候,在xml布局文件中定义了onClick()方法
但是在调用的时候,该方法没有被执行,最终导致应用闪退
错误代码:
/**
* OnClick for change page
*/
public void OnOne() {
Log.d("HSQ", "OnOne: ");
startActivity(new Intent(getApplicationContext(),OneActivity.class));
}
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The first line-->
<TableRow android:layout_weight="1">
<ImageView
...
android:onClick="OnOne"/>
...
</TableRow>
...
</TableLayout >
解决办法
在Java类调用对应的方法的时候,加上View作为参数
public void OnOne(View v) {
Log.d("HSQ", "OnOne: ");
startActivity(new Intent(getApplicationContext(),OneActivity.class));
}
虽然不知道原理,但是该问题已经得到解决。