android - My First App crashes when pressing a button -
i tried numerous times make app myfirstapp tutorial on developers.android (https://developer.android.com/training/basics/firstapp/index.html) work. i'm testing code on nexus 4 device stock android 4.4.2
my +problem app crashes everytime press send button , displaymessageactivity gets started. common cause crash of tutorial app displaymessageactivity not included in android manifest file, it's definitly included in mine. have no idea causes crash, here full code including logcat.
androidmanifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myfirstapp" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="8" 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.myfirstapp.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> mainactivity.java
package com.example.myfirstapp; import android.content.intent; import android.os.bundle; import android.support.v4.app.fragment; import android.support.v7.app.actionbaractivity; import android.view.layoutinflater; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.viewgroup; import android.widget.edittext; public class mainactivity extends actionbaractivity { 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) { 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.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; } } public void sendmessage(view view) { intent intent = new intent(this, displaymessageactivity.class); edittext edittext = (edittext) findviewbyid(r.id.enter_message); string message = edittext.gettext().tostring(); intent.putextra(extra_message,message); startactivity(intent); } } displaymessageactivity.java
package com.example.myfirstapp; import android.content.intent; import android.os.bundle; import android.support.v4.app.fragment; import android.support.v7.app.actionbaractivity; import android.view.layoutinflater; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.viewgroup; import android.widget.textview; public class displaymessageactivity extends actionbaractivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); intent intent = getintent(); string message = intent.getstringextra(mainactivity.extra_message); textview textview = new textview(this); textview.settextsize(40); textview.settext(message); 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; } } } activity_main.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" tools:context="com.example.myfirstapp.mainactivity" tools:ignore="mergerootframe" > <edittext android:id="@+id/enter_message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="@string/enter_message"/> <button android:id="@+id/button_send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_send" android:onclick="sendmessage"/> </linearlayout> fragment_main.xml
<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="com.example.myfirstapp.mainactivity$placeholderfragment" > </relativelayout> activity_display_message.xml
<framelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.myfirstapp.displaymessageactivity" tools:ignore="mergerootframe" /> fragment_display_message.xml
<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="com.example.myfirstapp.displaymessageactivity$placeholderfragment" > </relativelayout> logcat
03-18 15:27:35.195: e/androidruntime(12382): fatal exception: main 03-18 15:27:35.195: e/androidruntime(12382): process: com.example.myfirstapp, pid: 12382 03-18 15:27:35.195: e/androidruntime(12382): java.lang.runtimeexception: unable start activity componentinfo{com.example.myfirstapp/com.example.myfirstapp.displaymessageactivity}: java.lang.illegalargumentexception: no view found id 0x7f05003c (com.example.myfirstapp:id/container) fragment placeholderfragment{41e7f580 #0 id=0x7f05003c} 03-18 15:27:35.195: e/androidruntime(12382): @ android.app.activitythread.performlaunchactivity(activitythread.java:2195) 03-18 15:27:35.195: e/androidruntime(12382): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2245) 03-18 15:27:35.195: e/androidruntime(12382): @ android.app.activitythread.access$800(activitythread.java:135) 03-18 15:27:35.195: e/androidruntime(12382): @ android.app.activitythread$h.handlemessage(activitythread.java:1196) 03-18 15:27:35.195: e/androidruntime(12382): @ android.os.handler.dispatchmessage(handler.java:102) 03-18 15:27:35.195: e/androidruntime(12382): @ android.os.looper.loop(looper.java:136) 03-18 15:27:35.195: e/androidruntime(12382): @ android.app.activitythread.main(activitythread.java:5017) 03-18 15:27:35.195: e/androidruntime(12382): @ java.lang.reflect.method.invokenative(native method) 03-18 15:27:35.195: e/androidruntime(12382): @ java.lang.reflect.method.invoke(method.java:515) 03-18 15:27:35.195: e/androidruntime(12382): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) 03-18 15:27:35.195: e/androidruntime(12382): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) 03-18 15:27:35.195: e/androidruntime(12382): @ dalvik.system.nativestart.main(native method) 03-18 15:27:35.195: e/androidruntime(12382): caused by: java.lang.illegalargumentexception: no view found id 0x7f05003c (com.example.myfirstapp:id/container) fragment placeholderfragment{41e7f580 #0 id=0x7f05003c} 03-18 15:27:35.195: e/androidruntime(12382): @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:919) 03-18 15:27:35.195: e/androidruntime(12382): @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1104) 03-18 15:27:35.195: e/androidruntime(12382): @ android.support.v4.app.backstackrecord.run(backstackrecord.java:682) 03-18 15:27:35.195: e/androidruntime(12382): @ android.support.v4.app.fragmentmanagerimpl.execpendingactions(fragmentmanager.java:1467) 03-18 15:27:35.195: e/androidruntime(12382): @ android.support.v4.app.fragmentactivity.onstart(fragmentactivity.java:570) 03-18 15:27:35.195: e/androidruntime(12382): @ android.app.instrumentation.callactivityonstart(instrumentation.java:1171) 03-18 15:27:35.195: e/androidruntime(12382): @ android.app.activity.performstart(activity.java:5241) 03-18 15:27:35.195: e/androidruntime(12382): @ android.app.activitythread.performlaunchactivity(activitythread.java:2168) 03-18 15:27:35.195: e/androidruntime(12382): ... 11 more it great if spot error causes crash , me out edittext. lot in advance.
your problem here (displaymessageactivity):
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); intent intent = getintent(); string message = intent.getstringextra(mainactivity.extra_message); textview textview = new textview(this); textview.settextsize(40); textview.settext(message); setcontentview(textview); if (savedinstancestate == null) { getsupportfragmentmanager().begintransaction() .add(r.id.container, new placeholderfragment()).commit(); } } you trying add fragment r.id.container not part of activity's view hierarchy, since thing contains textview
Comments
Post a Comment