java - Unfortunately app stopped working in Android emulator -


this question has answer here:

the ui portion alone worked fine, later when wrote onclicklistener , ran code got error stating "unfortunately app stopped working".

i re-tried new project again getting these error. have attached xml, mainactivity class , logs well. kindly me out. have been stuck simple program whole week. :(

log:

 05-18 01:54:31.015: d/android :(1436): oncreate() event     05-18 01:54:31.015: d/androidruntime(1436): shutting down vm     05-18 01:54:31.015: w/dalvikvm(1436): threadid=1: thread exiting uncaught  exception (group=0xb2afaba8)     05-18 01:54:31.055: e/androidruntime(1436): fatal exception: main     05-18 01:54:31.055: e/androidruntime(1436): process: com.example.test3, pid: 1436     05-18 01:54:31.055: e/androidruntime(1436): java.lang.runtimeexception: unable start activity componentinfo{com.example.test3/com.example.test3.mainactivity}: java.lang.nullpointerexception     05-18 01:54:31.055: e/androidruntime(1436):     @ android.app.activitythread.performlaunchactivity(activitythread.java:2195)     05-18 01:54:31.055: e/androidruntime(1436):     @ android.app.activitythread.handlelaunchactivity(activitythread.java:2245)     05-18 01:54:31.055: e/androidruntime(1436):     @ android.app.activitythread.access$800(activitythread.java:135)     05-18 01:54:31.055: e/androidruntime(1436):     @ android.app.activitythread$h.handlemessage(activitythread.java:1196)     05-18 01:54:31.055: e/androidruntime(1436):     @ android.os.handler.dispatchmessage(handler.java:102)     05-18 01:54:31.055: e/androidruntime(1436):     @ android.os.looper.loop(looper.java:136)     05-18 01:54:31.055: e/androidruntime(1436):     @ android.app.activitythread.main(activitythread.java:5017)     05-18 01:54:31.055: e/androidruntime(1436):     @ java.lang.reflect.method.invokenative(native method)     05-18 01:54:31.055: e/androidruntime(1436):     @ java.lang.reflect.method.invoke(method.java:515)     05-18 01:54:31.055: e/androidruntime(1436):     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779)     05-18 01:54:31.055: e/androidruntime(1436):     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595)     05-18 01:54:31.055: e/androidruntime(1436):     @ dalvik.system.nativestart.main(native method)     05-18 01:54:31.055: e/androidruntime(1436): caused by: java.lang.nullpointerexception     05-18 01:54:31.055: e/androidruntime(1436):     @ com.example.test3.mainactivity.oncreate(mainactivity.java:25)     05-18 01:54:31.055: e/androidruntime(1436):     @ android.app.activity.performcreate(activity.java:5231)     05-18 01:54:31.055: e/androidruntime(1436):     @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087)     05-18 01:54:31.055: e/androidruntime(1436):     @ android.app.activitythread.performlaunchactivity(activitythread.java:2159)     05-18 01:54:31.055: e/androidruntime(1436):     ... 11 more     --- 

i not sure if wrong mainactivity.java.i attaching xml file , mainactivity.java here getting clear picture.

mainactivity.java:

package com.example.test3;  import android.app.activity; import android.os.bundle; import android.util.log; import android.view.view; import android.widget.button; import android.widget.textview; public class mainactivity extends activity {        string msg = "android : ";        int counter;        button add,sub;        textview display;         /** called when activity first created. */        @override        public void oncreate(bundle savedinstancestate) {           super.oncreate(savedinstancestate);           setcontentview(r.layout.activity_main);           log.d(msg, "the oncreate() event");           counter=0;           add=(button) findviewbyid(r.id.addbutton);           sub=(button) findviewbyid(r.id.subbutton);           display=(textview) findviewbyid(r.id.disp);           add.setonclicklistener(new view.onclicklistener() {              @override             public void onclick(view v) {                 // todo auto-generated method stub                 counter++;                 display.settext("count "+counter);              }         });          sub.setonclicklistener(new view.onclicklistener() {              @override             public void onclick(view v) {                 // todo auto-generated method stub                 counter--;                 display.settext("count "+counter);             }         });         }       } 

xml file :

        <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="com.example.test3.mainactivity$placeholderfragment" >      <button         android:id="@+id/subbutton"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_above="@+id/disp"         android:layout_torightof="@+id/addbutton"         android:text="sub 1" />      <button         android:id="@+id/addbutton"         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="14dp"         android:text="add 1" />      <textview         android:id="@+id/disp"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_below="@+id/addbutton"         android:layout_centerhorizontal="true"         android:layout_margintop="56dp"         android:text="count 0"         android:textappearance="?android:attr/textappearancelarge" />      </relativelayout> 

the cause (guessing) views belong fragment layout. should initialize views in fragment oncreateview below.

the other thing notice counter++ on both button click. should count-- on sub button click

@override public view oncreateview(layoutinflater inflater, viewgroup container,         bundle savedinstancestate) {     view rootview = inflater.inflate(r.layout.fragment_main, container,             false);       counter=0;       add=(button) rootview.findviewbyid(r.id.add);       sub=(button) rootview.findviewbyid(r.id.sub);       disp=(textview) rootview.findviewbyid(r.id.display);       add.setonclicklistener(new view.onclicklistener() {        @override       public void onclick(view v) {         // todo auto-generated method stub         counter++;         disp.settext("counter "+counter);       }      });      sub.setonclicklistener(new view.onclicklistener() {      @override     public void onclick(view v) {         // todo auto-generated method stub          counter++;          disp.settext("counter "+counter);      }    });      return rootview; } 

Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -