java - Getting "Unfortunately, my_app has stopped." because of NullPointerException at include in layout -


so i'm learning android coding , i've come across nullpointerexception , can't figure out why.

i know whatever causing app stop in line 26:

linearlayout billll = (linearlayout)findviewbyid(r.id.billlayout); 

how wrong? xml file has id i'm interested in. appreciate anyone's input on this. thanks

this tipcalc.java

package com.example.tipcalc;  import android.os.bundle; import android.app.activity; import android.text.editable; import android.text.textwatcher; import android.view.menu; import android.widget.edittext; import android.widget.linearlayout; import android.widget.textview;  public class tipcalc extends activity {      private double bill;     private double tiprate;     private double tip;      edittext billet;     edittext tiprateet;     edittext tipet;      textview billtv;     textview tipratetv;     textview tiptv;      linearlayout billll = (linearlayout)findviewbyid(r.id.billlayout);     linearlayout tipratell = (linearlayout)findviewbyid(r.id.tipratelayout);     linearlayout tipll = (linearlayout)findviewbyid(r.id.tiplayout);      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_tip_calc);          bill = 0;         tiprate = .15;         tip = bill*tiprate;          billet = (edittext)billll.findviewbyid(r.id.edittext);         tiprateet = (edittext)tipratell.findviewbyid(r.id.edittext);         tipet = (edittext)tipll.findviewbyid(r.id.edittext);          billtv = (textview)billll.findviewbyid(r.id.name);         tipratetv = (textview)tipratell.findviewbyid(r.id.name);         tiptv = (textview)tipll.findviewbyid(r.id.name);          billtv.settext(getstring(r.string.bill_total_name));         tipratetv.settext(getstring(r.string.tip_rate));         tiptv.settext(getstring(r.string.tip_name));          billet.addtextchangedlistener(billlistener);         tiprateet.addtextchangedlistener(tipratelistener);          billet.settext(string.format("%.02f",bill));         tiprateet.settext(string.format("%.02f",tiprate));         tipet.settext(string.format("%.02f",tip));     }      private textwatcher billlistener = new textwatcher(){          @override         public void aftertextchanged(editable s) {         }          @override         public void beforetextchanged(charsequence s, int start, int count,                 int after) {         }          @override         public void ontextchanged(charsequence s, int start, int before,                 int count) {             try{                 bill = double.parsedouble(s.tostring());             }             catch(numberformatexception e){                 bill = 0.0;             }             updatetip();         }      };      private textwatcher tipratelistener = new textwatcher(){          @override         public void aftertextchanged(editable s) {         }          @override         public void beforetextchanged(charsequence s, int start, int count,                 int after) {         }          @override         public void ontextchanged(charsequence s, int start, int before,                 int count) {             try{                 tiprate = double.parsedouble(s.tostring());             }             catch(numberformatexception e){                 tiprate = 0.15;             }             updatetip();         }      };      public void updatetip(){         tip = bill*tiprate;         tipet.settext(string.format("%.02f", tip));     }      @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.tip_calc, menu);         return true;     }  } 

this activity_tip_calc.xml

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical" >      <textview         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="@string/title" />      <include         android:id="@+id/billlayout"         layout="@layout/line" />      <include         android:id="@+id/tipratelayout"         layout="@layout/line" />      <include         android:id="@+id/tiplayout"         layout="@layout/line" />  </linearlayout> 

and line.xml

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:orientation="horizontal" >      <textview         android:id="@+id/name"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_weight="1" />      <edittext         android:id="@+id/edittext"         android:layout_width="0dip"         android:layout_height="wrap_content"         android:layout_weight="1"         android:inputtype="number" >          <requestfocus />     </edittext>  </linearlayout> 

here log well

07-31 18:40:30.225: e/androidruntime(31866): fatal exception: main 07-31 18:40:30.225: e/androidruntime(31866): java.lang.runtimeexception: unable instantiate activity componentinfo{com.example.tipcalc/com.example.tipcalc.tipcalc}: java.lang.nullpointerexception 07-31 18:40:30.225: e/androidruntime(31866):    @ android.app.activitythread.performlaunchactivity(activitythread.java:2137) 07-31 18:40:30.225: e/androidruntime(31866):    @ android.app.activitythread.handlelaunchactivity(activitythread.java:2261) 07-31 18:40:30.225: e/androidruntime(31866):    @ android.app.activitythread.access$600(activitythread.java:141) 07-31 18:40:30.225: e/androidruntime(31866):    @ android.app.activitythread$h.handlemessage(activitythread.java:1256) 07-31 18:40:30.225: e/androidruntime(31866):    @ android.os.handler.dispatchmessage(handler.java:99) 07-31 18:40:30.225: e/androidruntime(31866):    @ android.os.looper.loop(looper.java:137) 07-31 18:40:30.225: e/androidruntime(31866):    @ android.app.activitythread.main(activitythread.java:5103) 07-31 18:40:30.225: e/androidruntime(31866):    @ java.lang.reflect.method.invokenative(native method) 07-31 18:40:30.225: e/androidruntime(31866):    @ java.lang.reflect.method.invoke(method.java:525) 07-31 18:40:30.225: e/androidruntime(31866):    @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:737) 07-31 18:40:30.225: e/androidruntime(31866):    @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) 07-31 18:40:30.225: e/androidruntime(31866):    @ dalvik.system.nativestart.main(native method) 07-31 18:40:30.225: e/androidruntime(31866): caused by: java.lang.nullpointerexception 07-31 18:40:30.225: e/androidruntime(31866):    @ android.app.activity.findviewbyid(activity.java:1853) 07-31 18:40:30.225: e/androidruntime(31866):    @ com.example.tipcalc.tipcalc.<init>(tipcalc.java:26) 07-31 18:40:30.225: e/androidruntime(31866):    @ java.lang.class.newinstanceimpl(native method) 07-31 18:40:30.225: e/androidruntime(31866):    @ java.lang.class.newinstance(class.java:1130) 07-31 18:40:30.225: e/androidruntime(31866):    @ android.app.instrumentation.newactivity(instrumentation.java:1061) 07-31 18:40:30.225: e/androidruntime(31866):    @ android.app.activitythread.performlaunchactivity(activitythread.java:2128) 07-31 18:40:30.225: e/androidruntime(31866):    ... 11 more 

you need run line:

linearlayout billll = (linearlayout)findviewbyid(r.id.billlayout); 

and 2 line afrer it:

 linearlayout tipratell = (linearlayout)findviewbyid(r.id.tipratelayout); linearlayout tipll = (linearlayout)findviewbyid(r.id.tiplayout); 

only after inflated view in line:

setcontentview(r.layout.activity_tip_calc); 

and activity_tip_calc needs have billlayout in it. while activity doesn't have contentview have specified can't use findviewbyid view in it's layout.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -