android - Cannot find method onClick in the Activity even though it is there -


i have made button:

   <button          android:id="@+id/btn_dialog"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:text="@string/showdig"         android:onclick="onclick"         /> 

and onclick method in main activity

  public void onclick(view v){         log.d("click test", "log");         this.showdialog(0);     } 

but when click button get:

03-12 16:37:24.995: e/androidruntime(755): fatal exception: main 03-12 16:37:24.995: e/androidruntime(755): java.lang.illegalstateexception: not find method onclick(view) in activity class com.example.telefon.mainactivity onclick handler on view class android.widget.button id 'btn_dialog' 03-12 16:37:24.995: e/androidruntime(755):  @ android.view.view$1.onclick(view.java:2670) 03-12 16:37:24.995: e/androidruntime(755):  @ android.view.view.performclick(view.java:3110) 03-12 16:37:24.995: e/androidruntime(755):  @ android.view.view$performclick.run(view.java:11934) 03-12 16:37:24.995: e/androidruntime(755):  @ android.os.handler.handlecallback(handler.java:587) 03-12 16:37:24.995: e/androidruntime(755):  @ android.os.handler.dispatchmessage(handler.java:92) 03-12 16:37:24.995: e/androidruntime(755):  @ android.os.looper.loop(looper.java:132) 03-12 16:37:24.995: e/androidruntime(755):  @ android.app.activitythread.main(activitythread.java:4123) 03-12 16:37:24.995: e/androidruntime(755):  @ java.lang.reflect.method.invokenative(native method) 03-12 16:37:24.995: e/androidruntime(755):  @ java.lang.reflect.method.invoke(method.java:491) 03-12 16:37:24.995: e/androidruntime(755):  @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:841) 03-12 16:37:24.995: e/androidruntime(755):  @ com.android.internal.os.zygoteinit.main(zygoteinit.java:599) 03-12 16:37:24.995: e/androidruntime(755):  @ 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):  @ java.lang.classmembers.getconstructorormethod(classmembers.java:235) 

the whole 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 menu; adds items action bar if 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> 

problem onclick property. change property from

android:onclick="onclick" 

to

android:onclick="onclick" 

don't forget, it's case-sensitive if method's signature onclick won't match onclick.

public void onclick(view v){         log.d("click test", "log");         this.showdialog(0);     } 

now should works.


Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -