java - Android Application closing for no reason -


hi guys started learning android development 2 days ago.. trying out basic application increment , decrement. okay when i'm testing application on emulator, message "unfortunately, application " tried debugging code , found after declaring statement findviewbyid(), variables pointing null. , think that's reason error.. heres code, in mainactivity.java. please me out!

public class mainactivity extends actionbaractivity {  /* original code */ /* textview sum; button increment; button decrement; */ int total;   /* interpretation */ protected static textview sum; protected static button increment; protected static button decrement;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      this.findviewbyid();      total = 0;      increment.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view view) {             total++;             sum.settext("sum "+total);         }     });      decrement.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view view) {             total--;             sum.settext("sum "+total);         }     });      if (savedinstancestate == null) {         getsupportfragmentmanager().begintransaction()                 .add(r.id.container, new placeholderfragment())                 .commit();     } }  /* original code (moved fragment) */ /* private void findviewbyid() {     sum = (textview) findviewbyid(r.id.tv);          // sum points null     increment = (button) findviewbyid(r.id.inc);  //increment points null     decrement = (button) findviewbyid(r.id.dec);  // decrement points null } */ 

edit ::

placeholder fragment ::

 public static class placeholderfragment extends fragment {      public placeholderfragment() {     }      /* added */     context ctx = null;      @override     public view oncreateview(layoutinflater inflater, viewgroup container,             bundle savedinstancestate) {         view rootview;         rootview = inflater.inflate(r.layout.fragment_main, container, false);         return rootview;          /* added */         init();     }      /* added */     private void init()     {     final view v = getview();      ctx = getactivity().getapplicationcontext();          mainactivity.sum = (textview) v.findviewbyid(r.id.tv);          // sum points null         mainactivity.increment = (button) v.findviewbyid(r.id.inc);  //increment points null         mainactivity.decrement = (button) v.findviewbyid(r.id.dec);  // decrement points null     } } 

here layout ::

<linearlayout 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:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.vishal.simplecalc.mainactivity$placeholderfragment">  <textview     android:id="@+id/tv"     android:text="@string/sumview"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:textsize="30sp"     android:layout_gravity="center"     tools:context=".mainactivity" />  <button     android:id="@+id/inc"     android:text="@string/inc"     android:layout_width="200dp"     android:layout_height="wrap_content"     android:textsize="30sp"     android:layout_gravity="center"     android:textstyle="bold"     tools:context=".mainactivity"     />  <button     android:id="@+id/dec"     android:text="@string/dec"     android:layout_width="200dp"     android:layout_height="wrap_content"     android:textsize="30sp"     android:layout_gravity="center"     android:textstyle="bold"     tools:context=".mainactivity"     /> </linearlayout> 

just guessing here (you haven't added name xml layout).

i'm guessing you're confusing layouts , posted fragment_main.xml has content code believing held in activity_main, causes code return nulls when you're assigning findviewbyid()


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -