Attempt to invoke virtual method 'void android.widget.TextView.append(java.lang.CharSequence)' on a null object reference -


i'm unable data(text) edittext textview via button. logic should fine , can't find mistake.

i error click button triggeres "sendmessage(view v)". can't append text "chat" don't know why.

mainactivity.java:

package com.chattr.neonardo.chattr;  import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.util.log; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.textview; import android.widget.toast;  public class mainactivity extends appcompatactivity {      textview chat;     edittext message;     button send;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          chat = (textview) findviewbyid(r.id.chat);         message = (edittext) findviewbyid(r.id.message);         send = (button) findviewbyid(r.id.send);          log.d("test", "test2");  /*        send.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view view) {                 chat.append("\n" + message.gettext().tostring());             }         });*/     }      public void onmatchtap(view v) {         toast mytoast = toast.maketext(getapplicationcontext(), r.string.searching, toast.length_long);         mytoast.show();         setcontentview(r.layout.chat_main);         log.d("test", "test4");     }      public void sendmessage(view v) {         log.d("test", "test6");         chat.append("\n" + message.gettext().tostring());         log.d("test", "test7");     }  } 

i've tried onclicklistener didn't work commented out.

main_activity.xml:

<?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/activity_main" 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.chattr.neonardo.chattr.mainactivity">  <imageview     android:layout_width="wrap_content"     android:layout_height="wrap_content"     app:srccompat="@drawable/chattr"     android:id="@+id/imageview"     android:layout_alignparenttop="true"     android:layout_alignparentleft="true"     android:layout_alignparentstart="true" />  <button     android:id="@+id/button"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_alignparentbottom="true"     android:layout_centerhorizontal="true"     android:layout_marginbottom="91dp"     android:onclick="onmatchtap"     android:text="@string/start_chat" /> 

i'm working 2 different xml's because i'm changing scenes.

chat_main.xml:

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:weightsum="1">  <textview     android:id="@+id/chat"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:layout_weight="1.00"     android:autolink="web"     android:text="@string/chat_connection"     android:visibility="visible" />  <linearlayout     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:orientation="horizontal">      <edittext         android:id="@+id/message"         android:layout_width="301dp"         android:layout_height="wrap_content"         android:ems="10"         android:inputtype="text"         android:text="@string/defaultmessage" />      <button         android:id="@+id/send"         android:layout_width="wrap_content"         android:layout_height="match_parent"         android:elevation="0dp"         android:onclick="sendmessage"         android:text="send" /> </linearlayout> 

that's error:

    process: com.chattr.neonardo.chattr, pid: 24340 java.lang.illegalstateexception: not execute method android:onclick     @ android.support.v7.app.appcompatviewinflater$declaredonclicklistener.onclick(appcompatviewinflater.java:293)     @ android.view.view.performclick(view.java:5637)     @ android.view.view$performclick.run(view.java:22429)     @ android.os.handler.handlecallback(handler.java:751)     @ android.os.handler.dispatchmessage(handler.java:95)     @ android.os.looper.loop(looper.java:154)     @ android.app.activitythread.main(activitythread.java:6119)     @ java.lang.reflect.method.invoke(native method)     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:886)     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:776) caused by: java.lang.reflect.invocationtargetexception     @ java.lang.reflect.method.invoke(native method)     @ android.support.v7.app.appcompatviewinflater$declaredonclicklistener.onclick(appcompatviewinflater.java:288)     @ android.view.view.performclick(view.java:5637)      @ android.view.view$performclick.run(view.java:22429)      @ android.os.handler.handlecallback(handler.java:751)      @ android.os.handler.dispatchmessage(handler.java:95)      @ android.os.looper.loop(looper.java:154)      @ android.app.activitythread.main(activitythread.java:6119)      @ java.lang.reflect.method.invoke(native method)      @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:886)      @ com.android.internal.os.zygoteinit.main(zygoteinit.java:776)  caused by: java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.textview.append(java.lang.charsequence)' on null object reference     @ com.chattr.neonardo.chattr.mainactivity.sendmessage(mainactivity.java:47)     @ java.lang.reflect.method.invoke(native method)      @ android.support.v7.app.appcompatviewinflater$declaredonclicklistener.onclick(appcompatviewinflater.java:288)      @ android.view.view.performclick(view.java:5637)      @ android.view.view$performclick.run(view.java:22429)      @ android.os.handler.handlecallback(handler.java:751)      @ android.os.handler.dispatchmessage(handler.java:95)      @ android.os.looper.loop(looper.java:154)      @ android.app.activitythread.main(activitythread.java:6119)      @ java.lang.reflect.method.invoke(native method)      @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:886)      @ com.android.internal.os.zygoteinit.main(zygoteinit.java:776) 

remove setcontentview(r.layout.chat_main);

you should use intent open new activity inside onclick method.

intent intent = new intent(currentactivity.this,nextactivity.class); startactivity(intent); 

should be

public void onmatchtap(view v) {         toast mytoast = toast.maketext(getapplicationcontext(), r.string.searching, toast.length_long).show();         intent intent = new intent(mainactivity.this,chatmain.class);         startactivity(intent);         log.d("test", "test4");     } 

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -