java - NULLPOINT EXCEPTION ERROR during Simple Android app development -


this question has answer here:

i trying develop simple app using android adt , got null point exception error. returning null point exception error @ line 32

add.setonclicklistener(new view.onclicklistener() {          @override         public void onclick(view arg0) {             // todo auto-generated method stub             counter++;             display.settext("your total is" + counter);           }     }); 

my whole java code is

import android.support.v7.app.actionbaractivity; import android.support.v7.app.actionbar; import android.support.v4.app.fragment; 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.os.build; import android.widget.*;  public class mainactivity extends actionbaractivity {  int counter; button add , subtract; textview display;    @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     counter = 0;     add = (button) findviewbyid(r.id.badd1);     subtract = (button) findviewbyid(r.id.bsub1);     display = (textview) findviewbyid(r.id.textview1);      add.setonclicklistener(new view.onclicklistener() {          @override         public void onclick(view arg0) {             // todo auto-generated method stub             counter++;             display.settext("your total is" + counter);           }     });      subtract.setonclicklistener(new view.onclicklistener() {          @override         public void onclick(view arg0) {             // todo auto-generated method stub             counter--;             display.settext("your total is" + counter);          }     }); } @override public boolean oncreateoptionsmenu(menu menu) {     // inflate menu; adds items action bar if present.     getmenuinflater().inflate(r.menu.main, menu);     return true; }  } 

my xml code

<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:gravity="center_horizontal" 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.example.newboston.mainactivity$placeholderfragment" >  <textview     android:id="@+id/textview1"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:layout_gravity="center"     android:gravity="center"     android:text="your total 0"     android:textsize="45dp" />      <button         android:id="@+id/badd1"         android:layout_width="250dp"         android:layout_height="wrap_content"         android:layout_below="@+id/textview1"         android:layout_margintop="34dp"         android:layout_centerhorizontal="true"         android:text="add one"         android:textsize="20dp" />      <button         android:id="@+id/bsub1"         android:layout_width="250dp"         android:layout_height="wrap_content"         android:layout_alignleft="@+id/badd1"         android:layout_below="@+id/badd1"         android:layout_margintop="34dp"         android:text="subtract one"         android:textsize="20dp" />  </relativelayout> 

my logcat file

03-25 23:10:29.219: d/androidruntime(1211): shutting down vm 03-25 23:10:29.229: w/dalvikvm(1211): threadid=1: thread exiting uncaught exception (group=0xb2b10ba8) 03-25 23:10:29.279: e/androidruntime(1211): fatal exception: main 03-25 23:10:29.279: e/androidruntime(1211): process: com.example.newboston, pid: 1211 03-25 23:10:29.279: e/androidruntime(1211): java.lang.runtimeexception: unable start activity            componentinfo{com.example.newboston/com.example.newboston.mainactivity}: java.lang.nullpointerexception 03-25 23:10:29.279: e/androidruntime(1211):     @     android.app.activitythread.performlaunchactivity(activitythread.java:2195) 03-25 23:10:29.279: e/androidruntime(1211):     @      android.app.activitythread.handlelaunchactivity(activitythread.java:2245) 03-25 23:10:29.279: e/androidruntime(1211):     @ android.app.activitythread.access$800(activitythread.java:135) 03-25 23:10:29.279: e/androidruntime(1211):     @    android.app.activitythread$h.handlemessage(activitythread.java:1196) 03-25 23:10:29.279: e/androidruntime(1211):     @ android.os.handler.dispatchmessage(handler.java:102) 03-25 23:10:29.279: e/androidruntime(1211):     @ android.os.looper.loop(looper.java:136) 03-25 23:10:29.279: e/androidruntime(1211):     @ android.app.activitythread.main(activitythread.java:5017) 03-25 23:10:29.279: e/androidruntime(1211):     @ java.lang.reflect.method.invokenative(native method) 03-25 23:10:29.279: e/androidruntime(1211):     @ java.lang.reflect.method.invoke(method.java:515) 03-25 23:10:29.279: e/androidruntime(1211):     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) 03-25 23:10:29.279: e/androidruntime(1211):     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) 03-25 23:10:29.279: e/androidruntime(1211):     @ dalvik.system.nativestart.main(native method)  03-25 23:10:29.279: e/androidruntime(1211): caused by: java.lang.nullpointerexception 03-25 23:10:29.279: e/androidruntime(1211):     @ com.example.newboston.mainactivity.oncreate(mainactivity.java:32) 03-25 23:10:29.279: e/androidruntime(1211):     @ android.app.activity.performcreate(activity.java:5231) 03-25 23:10:29.279: e/androidruntime(1211):     @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) 03-25 23:10:29.279: e/androidruntime(1211):     @ android.app.activitythread.performlaunchactivity(activitythread.java:2159) 03-25 23:10:29.279: e/androidruntime(1211):     ... 11 more 03-25 23:10:36.449: i/process(1211): sending signal. pid: 1211 sig: 9 

if you're using more 1 layout file (e.g layout-land, layout-sw360dp), make sure elements reference exist on of them.


Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -