android - Use a button in the action bar with an intent to start a new activity -
i've got buttons in action bar so:
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <!-- search, should appear action button --> <item android:id="@+id/action_search" android:icon="@drawable/ic_action_search" android:title="@string/action_search" app:showasaction="ifroom" android:onclick="doublebet"/> <!-- settings, should in overflow --> <item android:id="@+id/action_settings" android:title="@string/action_settings" app:showasaction="never" />
i've added onclick action_search, launch new activity. here java launch activity.
public void doublebet(view view){ intent intent = new intent(this, displaymessageactivity.class); string x = "hello"; intent.putextra("key", x); //optional parameters startactivity(intent); }
i receive intent so, in displaymessageactivity class:
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); intent intent = getintent(); string message = intent.getstringextra("key"); }
however, app crashes when run it. cause of this?
here logcat:
try changing doublebet
method this:
public void doublebet(view view){ intent intent = new intent(this, displaymessageactivity.class); string x = "hello"; intent.putextra("key", x); //optional parameters startactivity(intent); }
to this:
public boolean doublebet(menuitem view){ intent intent = new intent(this, displaymessageactivity.class); string x = "hello"; intent.putextra("key", x); //optional parameters startactivity(intent); return true; }
Comments
Post a Comment