android - onClick method issue -
ok, going through tutorial of "myfirstapp" on devoloper.android.com site. on last tutorial , have did first chapter can sorta say. thing is, @ end of "start activity" lesson, @ end tells run app. when run it, appears clickable button on emulator, when click it, gets runtime error , forces close. have tried research on this, not clue on going on. error is: java.lang.illeglstateexception: not find method sendmessage(view) in activity class com.example.myfirstapp.mainactivity onclick handler on view class on android.widet.b going show code this:
mainactivity:
package com.example.myfirstapp; import android.os.bundle; import android.app.activity; import android.content.intent; import android.view.menu; import android.view.view; import android.widget.edittext; public class mainactivity extends activity { public final static string extra_message = "com.example.myfirstapp.message"; /** called when user clicks send button */ public void sendmessage(view view) { intent intent = new intent(this, displaymessageactivity.class); edittext edittext = (edittext) findviewbyid(r.id.edit_message); string message = edittext.gettext().tostring(); intent.putextra(extra_message, message); startactivity(intent); // in response button } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } } activity_display_message:
<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=".displaymessageactivity" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </relativelayout> displaymessageactivity.java:
package com.example.myfirstapp; import android.annotation.suppresslint; import android.annotation.targetapi; import android.app.actionbar; import android.app.activity; import android.content.intent; import android.os.build; import android.os.bundle; import android.support.v4.app.navutils; import android.view.menuitem; import android.widget.textview; public class displaymessageactivity extends activity { @suppresslint("newapi") @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // message intent intent intent = getintent(); string message = intent.getstringextra(mainactivity.extra_message); // create text view textview textview = new textview(this); textview.settextsize(40); textview.settext(message); // set text view activity layout setcontentview(textview); // make sure we're running on fry or higher use actionbar apis if (build.version.sdk_int >= build.version_codes.froyo) { // show button in action bar. getupactionbar().setdisplayhomeasupenabled(true); } } private actionbar getupactionbar() { // todo auto-generated method stub return null; } /** * set {@link android.app.actionbar}, if api available. */ @targetapi(build.version_codes.honeycomb) private void setupactionbar() { if (build.version.sdk_int >= build.version_codes.honeycomb) { getactionbar().setdisplayhomeasupenabled(true); } } @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case android.r.id.home: // id represents home or button. in case of // activity, button shown. use navutils allow users // navigate 1 level in application structure. // more details, see navigation pattern on android design: // // http://developer.android.com/design/patterns/navigation.html#up-vs-back // navutils.navigateupfromsametask(this); return true; } return super.onoptionsitemselected(item); } }
it looks misspelled function name in xml button onclick(). have uppercase "m" in java code
public void sendmessage(view view) { and lowercase "m" in xml
<button ... android:onclick="sendmessage"/> to comply java standards, change in xml
android:onclick="sendmessage"/>
Comments
Post a Comment