即使它存在,也无法在Activity中找到onClick方法

时间:2013-03-12 23:27:05

标签: android onclick

我做了一个按钮:

   <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>

2 个答案:

答案 0 :(得分:4)

问题是 onClick属性。从

改变你的财产
android:onClick="onCLick"

android:onClick="onClick"

别忘了,它区分大小写,所以如果您的方法的签名是 onCLick ,它就不会与 onClick <匹配/强>

public void onClick(View v){
        Log.d("click test", "log");
        this.showDialog(0);
    }

现在它应该有效。

答案 1 :(得分:0)

此外,如果有人遇到类似的问题,我发现我的XML onClick方法无法找到 - 这是因为它位于附加到XML /按钮的片段中,而不是活动。所以我这样做了:

   /*
   *   onInlineClick passthrough
   */
   public void onInlineClicked(View v) {
       myFragment.onInlinePressed(v);
}