NULL POINTER EXCEPTION in android application -


this simple activity takes message user , displays in second activity. getting java.lang null pointer exception.  data in file follows : 

// activity_main.xml // xml file main activity

<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=".mainactivity" >      <textview         android:id="@+id/textview1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignparentleft="true"         android:layout_alignparenttop="true"         android:layout_margintop="29dp"         android:text="enter message"         android:textsize="18dp" />      <edittext         android:id="@+id/edittext1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignparentleft="true"         android:layout_alignparentright="true"         android:layout_below="@+id/textview1"         android:layout_margintop="38dp"         android:ems="10" >          <requestfocus />     </edittext>      <button         android:id="@+id/button1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_below="@+id/edittext1"         android:layout_centerhorizontal="true"         android:layout_margintop="96dp"         android:text="send"         android:onclick="sendmessage" />  </relativelayout> 

// mainactivity.java // code main activity creates intent , pass value entered display activity

package com.example.simpleactivity;  import android.os.bundle; import android.app.activity; import android.content.intent; import android.view.menu; import android.view.view; import android.widget.edittext;  public class mainactivity extends activity {      public final static string extra_message = "com.example.simpleactivity.message";     @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);     }      public void sendmessage(view view) {          edittext textmessage = (edittext) findviewbyid(r.id.edittext1);         string message = textmessage.gettext().tostring();         intent intent = new intent(this, displaymessageactivity.class);         intent.putextra(extra_message, message);         startactivity(intent);     }      @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.main, menu);         return true;     }  } 

// activity_display_message.xml // xml file display activity

<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=".displaymessageactivity" >      <textview         android:id="@+id/textview2"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignparentleft="true"         android:layout_alignparenttop="true"         android:layout_marginleft="58dp"         android:layout_margintop="110dp"         android:text="large text"         android:textappearance="?android:attr/textappearancelarge" />  </relativelayout> 

// displaymessageactivity.java // display activity gets , intent , displays message

package com.example.simpleactivity;  import android.app.activity; import android.content.intent; import android.os.bundle; import android.view.menu; import android.widget.textview;  public class displaymessageactivity extends activity {      textview tv;     @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         //setcontentview(r.layout.activity_display_message);          intent intent = getintent();         string message = intent.getstringextra(mainactivity.extra_message);         tv = (textview) findviewbyid(r.id.textview2);         tv.settext(message);         setcontentview(tv);      }      @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.display_message, menu);         return true;     }  } 

// logcat follows:

08-09 18:23:08.812: i/choreographer(1520): skipped 37 frames!  application may doing work on main thread.  08-09 18:23:08.942: d/gralloc_goldfish(1520): emulator without gpu emulation detected.  08-09 18:23:13.262: d/androidruntime(1520): shutting down vm  08-09 18:23:13.262: w/dalvikvm(1520): threadid=1: thread exiting uncaught exception  (group=0x414c4700)  08-09 18:23:13.402: e/androidruntime(1520): fatal exception: main  08-09 18:23:13.402: e/androidruntime(1520): java.lang.runtimeexception: unable start activity componentinfo{com.example.simpleactivity/com.example.simpleactivity.displaymessageactivity}: java.lang.nullpointerexception  08-09 18:23:13.402: e/androidruntime(1520):     @ android.app.activitythread.performlaunchactivity(activitythread.java:2211)  08-09 18:23:13.402: e/androidruntime(1520):     @ android.app.activitythread.handlelaunchactivity(activitythread.java:2261)  08-09 18:23:13.402: e/androidruntime(1520):     @ android.app.activitythread.access$600(activitythread.java:141)  08-09 18:23:13.402: e/androidruntime(1520):     @ android.app.activitythread$h.handlemessage(activitythread.java:1256)  08-09 18:23:13.402: e/androidruntime(1520):     @ android.os.handler.dispatchmessage(handler.java:99)  08-09 18:23:13.402: e/androidruntime(1520):     @ android.os.looper.loop(looper.java:137)  08-09 18:23:13.402: e/androidruntime(1520):     @ android.app.activitythread.main(activitythread.java:5103)  08-09 18:23:13.402: e/androidruntime(1520):     @ java.lang.reflect.method.invokenative(native method)  08-09 18:23:13.402: e/androidruntime(1520):     @ java.lang.reflect.method.invoke(method.java:525)  08-09 18:23:13.402: e/androidruntime(1520):     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:737)  08-09 18:23:13.402: e/androidruntime(1520):     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553)  08-09 18:23:13.402: e/androidruntime(1520):     @ dalvik.system.nativestart.main(native method)  08-09 18:23:13.402: e/androidruntime(1520): caused by: java.lang.nullpointerexception  08-09 18:23:13.402: e/androidruntime(1520):     @ com.example.simpleactivity.displaymessageactivity.oncreate(displaymessageactivity.java:20)  08-09 18:23:13.402: e/androidruntime(1520):     @ android.app.activity.performcreate(activity.java:5133)  08-09 18:23:13.402: e/androidruntime(1520):     @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087)  08-09 18:23:13.402: e/androidruntime(1520):     @ android.app.activitythread.performlaunchactivity(activitythread.java:2175)  08-09 18:23:13.402: e/androidruntime(1520):     ... 11 more  08-09 18:23:17.812: i/process(1520): sending signal. pid: 1520 sig: 9 

in displaymessageactivity oncreate() method :

 @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         //setcontentview(r.layout.activity_display_message);          intent intent = getintent();         string message = intent.getstringextra(mainactivity.extra_message);         tv = (textview) findviewbyid(r.id.textview2);         tv.settext(message);         setcontentview(tv);      } 

uncomment first setcontentview() , comment last setcontentview() below :

@override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_display_message);          intent intent = getintent();         string message = intent.getstringextra(mainactivity.extra_message);         tv = (textview) findviewbyid(r.id.textview2);         tv.settext(message);         //setcontentview(tv);      } 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -