java - A Force Close whenever I click the start button on my app on my phone? -
every time click on start button in android app, consistently force close. there 0 errors in code, i'm confused why happening.
fragment_main_menu.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.app.whosesloganisthat.mainmenu$placeholderfragment" > <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <button android:id="@+id/buttonstart" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignleft="@+id/textview1" android:layout_below="@+id/textview1" android:layout_margintop="37dp" android:text="@string/start" android:onclick="sendmessage" /> </relativelayout>
fragment_easy_level_info.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.app.whosesloganisthat.easylevelinfo$placeholderfragment" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </relativelayout>
mainmenu.java:
package com.app.whosesloganisthat; import android.support.v7.app.actionbaractivity; import android.support.v7.app.actionbar; import android.support.v4.app.fragment; import android.content.intent; 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.widget.edittext; import android.os.build; public class mainmenu extends actionbaractivity { public final static string extra_message = "com.app.whosesloganisthat.message"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main_menu); 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, 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); } /** called when user clicks send button */ public void sendmessage(view view) { intent intent = new intent(this, easylevelinfo.class); edittext edittext = (edittext) findviewbyid(r.id.container); string message = edittext.gettext().tostring(); intent.putextra(extra_message, message); startactivity(intent); // in response button } /** * 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_menu, container, false); return rootview; } } }
easylevelinfo.java:
package com.app.whosesloganisthat; import android.support.v7.app.actionbaractivity; import android.support.v7.app.actionbar; import android.support.v4.app.fragment; import android.content.intent; 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.widget.textview; import android.os.build; public class easylevelinfo extends actionbaractivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_easy_level_info); intent intent = getintent(); string message = intent.getstringextra(mainmenu.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.easy_level_info, 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_easy_level_info, container, false); return rootview; } } }
androidmanifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.app.whosesloganisthat" 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.app.whosesloganisthat.mainmenu" 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.app.whosesloganisthat.easylevelinfo" android:label="@string/title_activity_easy_level_info" > </activity> <activity android:name=".toactivity" android:label="@string/app_name"> </activity> </application> </manifest>
logcat:
06-27 18:31:12.307: e/androidruntime(16299): fatal exception: main 06-27 18:31:12.307: e/androidruntime(16299): process: com.app.whosesloganisthat, pid: 16299 06-27 18:31:12.307: e/androidruntime(16299): java.lang.illegalstateexception: not execute method of activity 06-27 18:31:12.307: e/androidruntime(16299): @ android.view.view$1.onclick(view.java) 06-27 18:31:12.307: e/androidruntime(16299): @ android.view.view.performclick(view.java) 06-27 18:31:12.307: e/androidruntime(16299): @ android.view.view$performclick.run(view.java) 06-27 18:31:12.307: e/androidruntime(16299): @ android.os.handler.handlecallback(handler.java) 06-27 18:31:12.307: e/androidruntime(16299): @ android.os.handler.dispatchmessage(handler.java) 06-27 18:31:12.307: e/androidruntime(16299): @ android.os.looper.loop(looper.java) 06-27 18:31:12.307: e/androidruntime(16299): @ android.app.activitythread.main(activitythread.java) 06-27 18:31:12.307: e/androidruntime(16299): @ java.lang.reflect.method.invokenative(native method) 06-27 18:31:12.307: e/androidruntime(16299): @ java.lang.reflect.method.invoke(method.java:515) 06-27 18:31:12.307: e/androidruntime(16299): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java) 06-27 18:31:12.307: e/androidruntime(16299): @ com.android.internal.os.zygoteinit.main(zygoteinit.java) 06-27 18:31:12.307: e/androidruntime(16299): @ dalvik.system.nativestart.main(native method) 06-27 18:31:12.307: e/androidruntime(16299): caused by: java.lang.reflect.invocationtargetexception 06-27 18:31:12.307: e/androidruntime(16299): @ java.lang.reflect.method.invokenative(native method) 06-27 18:31:12.307: e/androidruntime(16299): @ java.lang.reflect.method.invoke(method.java:515) 06-27 18:31:12.307: e/androidruntime(16299): ... 12 more 06-27 18:31:12.307: e/androidruntime(16299): caused by: java.lang.nullpointerexception 06-27 18:31:12.307: e/androidruntime(16299): @ com.app.whosesloganisthat.mainmenu.sendmessage(mainmenu.java:51) 06-27 18:31:12.307: e/androidruntime(16299): ... 14 more
also, let me know if think forgot add in other code view. help!
try this..
remove below lines both mainmenu
, easylevelinfo
if (savedinstancestate == null) { getsupportfragmentmanager().begintransaction() .add(r.id.container, new placeholderfragment()).commit(); }
and mainmenu.java
change
setcontentview(r.layout.activity_main_menu);
to
setcontentview(r.layout.fragment_main_menu);
because button
onclick android:onclick="sendmessage"
work on activity
and remove
setcontentview(r.layout.activity_easy_level_info);
from easylevelinfo.java
because programmatically setting textview in view no need setcontentview(textview);
edit
<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.app.whosesloganisthat.mainmenu$placeholderfragment" > <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <edittext android:id="@+id/container" android:layout_width="match_parent" android:layout_height="55dp" /> <button android:id="@+id/buttonstart" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignleft="@+id/textview1" android:layout_below="@+id/textview1" android:layout_margintop="37dp" android:text="@string/start" android:onclick="sendmessage" /> </relativelayout>
Comments
Post a Comment