-1

我做了一个按钮:

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

2 回答 2

4

问题是onClick 属性。更改您的财产

android:onClick="onCLick"

android:onClick="onClick"

不要忘记,它是区分大小写的,所以如果您的方法的签名是onCLick,它将与onClick不匹配。

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

现在它应该可以工作了。

于 2013-03-12T23:28:18.113 回答
0

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

   /*
   *   onInlineClick passthrough
   */
   public void onInlineClicked(View v) {
       myFragment.onInlinePressed(v);
}
于 2016-03-04T06:18:57.640 回答