我做了一个按钮:
<Button
android:id="@+id/btn_dialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/showDig"
android:onClick="onCLick"
/>
以及onClick
主要活动中的方法
public void onClick(View v){
Log.d("click test", "log");
this.showDialog(0);
}
但是当我单击按钮时,我得到:
03-12 16:37:24.995: E/AndroidRuntime(755): FATAL EXCEPTION: main
03-12 16:37:24.995: E/AndroidRuntime(755): java.lang.IllegalStateException: Could not find a method onCLick(View) in the activity class com.example.telefon.MainActivity for onClick handler on view class android.widget.Button with id 'btn_dialog'
03-12 16:37:24.995: E/AndroidRuntime(755): at android.view.View$1.onClick(View.java:2670)
03-12 16:37:24.995: E/AndroidRuntime(755): at android.view.View.performClick(View.java:3110)
03-12 16:37:24.995: E/AndroidRuntime(755): at android.view.View$PerformClick.run(View.java:11934)
03-12 16:37:24.995: E/AndroidRuntime(755): at android.os.Handler.handleCallback(Handler.java:587)
03-12 16:37:24.995: E/AndroidRuntime(755): at android.os.Handler.dispatchMessage(Handler.java:92)
03-12 16:37:24.995: E/AndroidRuntime(755): at android.os.Looper.loop(Looper.java:132)
03-12 16:37:24.995: E/AndroidRuntime(755): at android.app.ActivityThread.main(ActivityThread.java:4123)
03-12 16:37:24.995: E/AndroidRuntime(755): at java.lang.reflect.Method.invokeNative(Native Method)
03-12 16:37:24.995: E/AndroidRuntime(755): at java.lang.reflect.Method.invoke(Method.java:491)
03-12 16:37:24.995: E/AndroidRuntime(755): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
03-12 16:37:24.995: E/AndroidRuntime(755): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
03-12 16:37:24.995: E/AndroidRuntime(755): at dalvik.system.NativeStart.main(Native Method)
03-12 16:37:24.995: E/AndroidRuntime(755): Caused by: java.lang.NoSuchMethodException: onCLick [class android.view.View]
03-12 16:37:24.995: E/AndroidRuntime(755): at java.lang.ClassMembers.getConstructorOrMethod(ClassMembers.java:235)
整个 xml 和 .java
package com.example.telefon;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String s;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onClick(View v){
Log.d("click test", "log");
this.showDialog(0);
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/btn_dialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/showDig"
android:onClick="onCLick"
/>
</RelativeLayout>