android - Unable to start a sub activity using intent -
i trying start sub activity main activity using intents using android sdk version 4. doing so, results in error during runtime.
screen error received emulator - "unfortunately myfirstapp has stopped"
logcat error - "could not find class 'com.example.firstapp.displaymessageactivity', referenced method com.example.firstapp.mainactivity.sendmessage"
here "com.example.firstapp.displaymessageactivity" sub activity called main activity "com.example.firstapp.mainactivity"
same file content:
com.example.firstapp.mainactivity
public class mainactivity extends activity { public final static string extra_message = "com.example.myfirstapp.message"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); if (savedinstancestate == null) { getfragmentmanager().begintransaction() .add(r.id.container, new placeholderfragment()) .commit(); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } /** * placeholder fragment containing simple view. */ public static class placeholderfragment extends fragment { public placeholderfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_main, container, false); return rootview; } } /** 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); try { startactivity(intent); } catch (activitynotfoundexception e) { e.printstacktrace(); } catch(exception e) { e.printstacktrace(); } } }
com.example.firstapp.displaymessageactivity
public class displaymessageactivity extends actionbaractivity { @override public 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); } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } /** * placeholder fragment containing simple view. */ public static class placeholderfragment extends fragment { public placeholderfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_display_message, container, false); return rootview; } } }
android manifest file:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.firstapp" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="19" android:targetsdkversion="19" /> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name="com.example.firstapp.mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name="com.example.myfirstapp.displaymessageactivity" android:label="@string/title_activity_display_message" android:parentactivityname="com.example.myfirstapp.mainactivity" > <meta-data android:name="android.support.parent_activity" android:value="com.example.myfirstapp.mainactivity" /> </activity> </application> </manifest>
ps: trying follow tutorial content here
any appreciated. thanks
-deep
in manifest have activity named : android:name="com.example.myfirstapp.displaymessageactivity"
actually package name : com.example.firstapp
so manifest must declasre activity as:
<activity android:name="com.example.firstapp.displaymessageactivity" android:label="@string/title_activity_display_message" android:parentactivityname="com.example.firstapp.mainactivity" > <meta-data android:name="android.support.parent_activity" android:value="com.example.firstapp.mainactivity" /> </activity>
silly frustrating error. hope learn focus mistake.
Comments
Post a Comment