java - NullPointerException causing app to instantly crash -
my apologies bad question title.
my problem app keeps instantly crashing once action performed. when read error, i'm seeing nullpointerexception
reason crashing.
the error states takes place in main.java
@ line 48. here full main.java
:
package com.fuzzyapplications.tutorial.youtube.app; import android.content.intent; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.edittext; public class main extends actionbaractivity { public static final string extra_message = "com.fuzzyapplications.practice.app.message"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } @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); } public void sendmessage(view view){ intent intent = new intent(this, displaymessageactivity.class); edittext edittext = (edittext) findviewbyid(r.string.edit_message); string message = edittext.gettext().tostring(); intent.putextra(extra_message, message); startactivity(intent); }
}
also, here activity should activated (once button pushed
package com.fuzzyapplications.tutorial.youtube.app; import android.content.intent; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.view.menu; import android.view.menuitem; 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(main.extra_message); textview textview = new textview(this); textview.settextsize(40); textview.settext(message); setcontentview(textview); } @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); } }
hope enough information you! new android i'm not sure how information should given problem, if don't have enough information please let me know! again.
edit: logcat info , layout info added!
logcat:
fatal exception: main java.lang.illegalstateexception: not execute method of activity @ android.view.view$1.onclick(view.java:3802) @ android.view.view.performclick(view.java:4437) @ android.view.view$performclick.run(view.java:18365) @ android.os.handler.handlecallback(handler.java:725) @ android.os.handler.dispatchmessage(handler.java:92) @ android.os.looper.loop(looper.java:137) @ android.app.activitythread.main(activitythread.java:5285) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1102) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:869) @ dalvik.system.nativestart.main(native method) caused by: java.lang.reflect.invocationtargetexception @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ android.view.view$1.onclick(view.java:3797) at android.view.view.performclick(view.java:4437) at android.view.view$performclick.run(view.java:18365) at android.os.handler.handlecallback(handler.java:725) at android.os.handler.dispatchmessage(handler.java:92) at android.os.looper.loop(looper.java:137) at android.app.activitythread.main(activitythread.java:5285) at java.lang.reflect.method.invokenative(native method) at java.lang.reflect.method.invoke(method.java:511) at com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1102) at com.android.internal.os.zygoteinit.main(zygoteinit.java:869) at dalvik.system.nativestart.main(native method) caused by: java.lang.nullpointerexception @ com.fuzzyapplications.tutorial.youtube.app.main.sendmessage(main.java:49) at java.lang.reflect.method.invokenative(native method) at java.lang.reflect.method.invoke(method.java:511) at android.view.view$1.onclick(view.java:3797) at android.view.view.performclick(view.java:4437) at android.view.view$performclick.run(view.java:18365) at android.os.handler.handlecallback(handler.java:725) at android.os.handler.dispatchmessage(handler.java:92) at android.os.looper.loop(looper.java:137) at android.app.activitythread.main(activitythread.java:5285) at java.lang.reflect.method.invokenative(native method) at java.lang.reflect.method.invoke(method.java:511) at com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1102) at com.android.internal.os.zygoteinit.main(zygoteinit.java:869) at dalvik.system.nativestart.main(native method)
layout files:
main:
<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_weight="1" android:layout_width="0dp" 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_send" android:onclick="sendmessage" /> </linearlayout>
messagesendactivity layout:
<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:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" tools:context="com.fuzzyapplications.practice.app.displaymessageactivity"> <textview android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </relativelayout>
you're trying refer reference string
, doesn't exist in r.java, instead of reference id
, declared in xml layout android:id="@+id/edit_message". causes nullpointerexception. use:
edittext edittext = (edittext) findviewbyid(r.id.edit_message);
instead of:
edittext edittext = (edittext) findviewbyid(r.string.edit_message);
Comments
Post a Comment