error Android myapp has stop -
sorry, have small question android. want make app, user input message , press button, start new activity , display message! have problem , have error : unfortunately, myapp has stop!
this main activity:
package com.example.mainactivity; import com.example.myapp.r; import android.support.v7.app.actionbaractivity; import android.support.v7.app.actionbar; import android.support.v4.app.fragment; import android.os.bundle; import android.view.layoutinflater; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.viewgroup; import android.os.build; import android.widget.edittext; import android.content.intent; public class mainactivity extends actionbaractivity { public final static string extra_message="com.example.mainactivity.message"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); if (savedinstancestate == null) { getsupportfragmentmanager().begintransaction() .add(r.id.container, new placeholderfragment()) .commit(); } } public void sendmessage(view view) { intent intent = new intent(this, displaymessage.class); edittext edittext = (edittext) findviewbyid(r.id.edit_message); string message = edittext.gettext().tostring(); intent.putextra(extra_message, message); startactivity(intent); } @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; } } }
here, displaymessage.java
package com.example.mainactivity; import com.example.myapp.r; import android.support.v7.app.actionbaractivity; import android.support.v7.app.actionbar; import android.support.v4.app.fragment; import android.os.bundle; import android.view.layoutinflater; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.viewgroup; import android.os.build; import android.content.intent; import android.widget.edittext; import android.view.view; import android.widget.textview; public class displaymessage extends actionbaractivity { @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); if (savedinstancestate == null) { getsupportfragmentmanager().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.display_message, 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_display_message, container, false); return rootview; } } }
androidmanifest :
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myapp" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="8" android:targetsdkversion="18" /> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name="com.example.myapp.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.myapp.displaymessage" android:label="@string/title_activity_display_message" android:parentactivityname="com.example.myapp.mainactivity"> <meta-data android:name="android.support.parent_activity" android:value="com.example.myapp.mainactivity" /> </activity> </application> </manifest>
fragment_main layout:
<linearlayout 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:orientation="horizontal"> <edittext android:id="@+id/edit_message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="@string/edit_message" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_name" android:onclick="sendmessage" /> </linearlayout>
here, logcat:
06-03 22:38:09.386: e/androidruntime(18119): fatal exception: main 06-03 22:38:09.386: e/androidruntime(18119): java.lang.runtimeexception: unable instantiate activity componentinfo{com.example.myapp/com.example.myapp.mainactivity}: java.lang.classnotfoundexception: com.example.myapp.mainactivity 06-03 22:38:09.386: e/androidruntime(18119): @ android.app.activitythread.performlaunchactivity(activitythread.java:1984) 06-03 22:38:09.386: e/androidruntime(18119): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2085) 06-03 22:38:09.386: e/androidruntime(18119): @ android.app.activitythread.access$600(activitythread.java:130) 06-03 22:38:09.386: e/androidruntime(18119): @ android.app.activitythread$h.handlemessage(activitythread.java:1196) 06-03 22:38:09.386: e/androidruntime(18119): @ android.os.handler.dispatchmessage(handler.java:99) 06-03 22:38:09.386: e/androidruntime(18119): @ android.os.looper.loop(looper.java:137) 06-03 22:38:09.386: e/androidruntime(18119): @ android.app.activitythread.main(activitythread.java:4747) 06-03 22:38:09.386: e/androidruntime(18119): @ java.lang.reflect.method.invokenative(native method) 06-03 22:38:09.386: e/androidruntime(18119): @ java.lang.reflect.method.invoke(method.java:511) 06-03 22:38:09.386: e/androidruntime(18119): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:786) 06-03 22:38:09.386: e/androidruntime(18119): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) 06-03 22:38:09.386: e/androidruntime(18119): @ dalvik.system.nativestart.main(native method) 06-03 22:38:09.386: e/androidruntime(18119): caused by: java.lang.classnotfoundexception: com.example.myapp.mainactivity 06-03 22:38:09.386: e/androidruntime(18119): @ dalvik.system.basedexclassloader.findclass(basedexclassloader.java:61) 06-03 22:38:09.386: e/androidruntime(18119): @ java.lang.classloader.loadclass(classloader.java:501) 06-03 22:38:09.386: e/androidruntime(18119): @ java.lang.classloader.loadclass(classloader.java:461) 06-03 22:38:09.386: e/androidruntime(18119): @ android.app.instrumentation.newactivity(instrumentation.java:1053) 06-03 22:38:09.386: e/androidruntime(18119): @ android.app.activitythread.performlaunchactivity(activitythread.java:1975) 06-03 22:38:09.386: e/androidruntime(18119): ... 11 more
your package in java file doesn't match in manifest.xml
package com.example.mainactivity;
in java file. but
android:name="com.example.myapp.mainactivity"
in manifest. want change java file to
package com.example.myapp;
you going have more problems after because trying access view
s fragment layout have inflated activity layout. want check that, also.
Comments
Post a Comment