Android Studio - java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener -
i created new activity in android studio app , have button performs action when click. not sure what's causing error happens when run activity. if remove onclick code, activity runs. help. i'm getting error:
e/androidruntime: fatal exception: main process: com.masesk.kalkulator, pid: 4102 java.lang.runtimeexception: unable start activity componentinfo{com.masesk.kalkulator/com.masesk.kalkulator.main2activity}: java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.button.setonclicklistener(android.view.view$onclicklistener)' on null object reference @ android.app.activitythread.performlaunchactivity(activitythread.java:2416) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2476) @ android.app.activitythread.-wrap11(activitythread.java) @ android.app.activitythread$h.handlemessage(activitythread.java:1344) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:148) @ android.app.activitythread.main(activitythread.java:5417) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616) caused by: java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.button.setonclicklistener(android.view.view$onclicklistener)' on null object reference @ com.masesk.kalkulator.main2activity.oncreate(main2activity.java:29) @ android.app.activity.performcreate(activity.java:6237) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1107) @ android.app.activitythread.performlaunchactivity(activitythread.java:2369) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2476) @ android.app.activitythread.-wrap11(activitythread.java) @ android.app.activitythread$h.handlemessage(activitythread.java:1344) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:148) @ android.app.activitythread.main(activitythread.java:5417) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616)
this main2activity.java code:
package com.masesk.kalkulator; import android.os.bundle; import android.support.design.widget.floatingactionbutton; import android.support.design.widget.snackbar; import android.support.v7.app.appcompatactivity; import android.support.v7.widget.toolbar; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.textview; import android.widget.togglebutton; public class main2activity extends appcompatactivity { private togglebutton texttobinary; private button trace; private edittext text; private edittext binary; private string textinput; private string binaryinput; stringbuilder binarytext = new stringbuilder(); @override protected void oncreate(bundle savedinstancestate) { this.texttobinary = (togglebutton)findviewbyid(r.id.texttobinary); this.trace = (button)findviewbyid(r.id.trace); this.text = (edittext)findviewbyid(r.id.text); this.binary = (edittext)findviewbyid(r.id.binary); trace.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { if(texttobinary.isactivated() == false){ textinput = text.gettext().tostring(); byte[] bytes = textinput.getbytes(); stringbuilder binary = new stringbuilder(); (byte b : bytes) { int val = b; (int = 0; < 8; i++) { binarytext.append((val & 128) == 0 ? 0 : 1); val <<= 1; } binarytext.append(' '); } } } }); super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main2); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); } @override public void onresume(){ this.binary = (edittext)findviewbyid(r.id.binary); binary.settext(binarytext.tostring(), textview.buffertype.editable); } }
and here xml code
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/content_main2" 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" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.masesk.kalkulator.main2activity" tools:showin="@layout/activity_main2"> <togglebutton android:text="togglebutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_alignparentstart="true" android:id="@+id/texttobinary" android:layout_alignparentend="true" android:textoff="binary text" android:texton="text binary" /> <textview android:text="text:" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/texttobinary" android:layout_alignparentstart="true" android:id="@+id/textview2" android:layout_alignparentend="true" /> <edittext android:background="@drawable/back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputtype="textmultiline" android:ems="10" android:layout_below="@+id/textview2" android:layout_alignparentstart="true" android:id="@+id/text" android:layout_alignparentend="true" /> <textview android:text="binary code:" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/text" android:layout_alignparentstart="true" android:id="@+id/textview3" /> <edittext android:background="@drawable/back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputtype="textmultiline" android:ems="10" android:layout_below="@+id/textview3" android:layout_alignparentstart="true" android:id="@+id/binary" android:layout_alignparentend="true" /> <button android:text="convert" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/binary" android:layout_alignparentstart="true" android:layout_margintop="14dp" android:id="@+id/trace" android:layout_alignparentend="true" /> </relativelayout>
in oncreate
method, should first put
super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main2);
this because view not inflated until have set setcontentview
. means findviewbyid
call before return null
.
additionally, must set super.onresume(...)
call @ very beginning of onresume()
, android documentation states. goes other lifecycle methods well. should solve settext
problem.
Comments
Post a Comment