android - eclipse NullPointerRxception -
can pls me: don't find i've done wrong in activity.
this error logcat:
11-27 10:37:25.551: e/androidruntime(26862): fatal exception: main 11-27 10:37:25.551: e/androidruntime(26862): process: pack.coderzheaven, pid: 26862 11-27 10:37:25.551: e/androidruntime(26862): java.lang.runtimeexception: unable instantiate activity componentinfo{pack.coderzheaven/pack.coderzheaven.bestellung}: java.lang.nullpointerexception 11-27 10:37:25.551: e/androidruntime(26862): @ android.app.activitythread.performlaunchactivity(activitythread.java:2231) 11-27 10:37:25.551: e/androidruntime(26862): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2363) 11-27 10:37:25.551: e/androidruntime(26862): @ android.app.activitythread.access$900(activitythread.java:161) 11-27 10:37:25.551: e/androidruntime(26862): @ android.app.activitythread$h.handlemessage(activitythread.java:1265) 11-27 10:37:25.551: e/androidruntime(26862): @ android.os.handler.dispatchmessage(handler.java:102) 11-27 10:37:25.551: e/androidruntime(26862): @ android.os.looper.loop(looper.java:157) 11-27 10:37:25.551: e/androidruntime(26862): @ android.app.activitythread.main(activitythread.java:5356) 11-27 10:37:25.551: e/androidruntime(26862): @ java.lang.reflect.method.invokenative(native method) 11-27 10:37:25.551: e/androidruntime(26862): @ java.lang.reflect.method.invoke(method.java:515) 11-27 10:37:25.551: e/androidruntime(26862): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1265) 11-27 10:37:25.551: e/androidruntime(26862): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1081) 11-27 10:37:25.551: e/androidruntime(26862): @ dalvik.system.nativestart.main(native method) 11-27 10:37:25.551: e/androidruntime(26862): caused by: java.lang.nullpointerexception 11-27 10:37:25.551: e/androidruntime(26862): @ android.app.activity.findviewbyid(activity.java:1965) 11-27 10:37:25.551: e/androidruntime(26862): @ pack.coderzheaven.bestellung.<init>(bestellung.java:16) 11-27 10:37:25.551: e/androidruntime(26862): @ java.lang.class.newinstanceimpl(native method) 11-27 10:37:25.551: e/androidruntime(26862): @ java.lang.class.newinstance(class.java:1208) 11-27 10:37:25.551: e/androidruntime(26862): @ android.app.instrumentation.newactivity(instrumentation.java:1079) 11-27 10:37:25.551: e/androidruntime(26862): @ android.app.activitythread.performlaunchactivity(activitythread.java:2222) 11-27 10:37:25.551: e/androidruntime(26862): ... 11 more
and java-file:
package pack.coderzheaven; import java.text.decimalformat; import android.app.activity; import android.os.bundle; import android.widget.numberpicker; import android.widget.numberpicker.onvaluechangelistener; import android.widget.textview; public class bestellung extends activity implements numberpicker.onvaluechangelistener { decimalformat f = new decimalformat("0.00"); final textview bierpreis = (textview)findviewbyid(r.id.bierpreis); final textview seidlpreis = (textview)findviewbyid(r.id.seidlpreis); final textview weinpreis = (textview)findviewbyid(r.id.weinpreis); final textview spritzerpreis = (textview)findviewbyid(r.id.spritzerpreis); final textview weinmixpreis = (textview)findviewbyid(r.id.weinmixpreis); final textview colapreis = (textview)findviewbyid(r.id.colapreis); final textview limoklpreis = (textview)findviewbyid(r.id.limoklpreis); final textview limoklgespreis = (textview)findviewbyid(r.id.limoklgespreis); final textview limogrgespreis = (textview)findviewbyid(r.id.limogrgespreis); final textview biererg = (textview)findviewbyid(r.id.biererg); final textview seidlerg = (textview)findviewbyid(r.id.seidlerg); final textview weinerg = (textview)findviewbyid(r.id.weinerg); final textview spritzererg = (textview)findviewbyid(r.id.spritzererg); final textview weinmixerg = (textview)findviewbyid(r.id.weinmixerg); final textview colaerg = (textview)findviewbyid(r.id.colaerg); final textview limoklerg = (textview)findviewbyid(r.id.limoklerg); final textview limoklgeserg = (textview)findviewbyid(r.id.limoklgeserg); final textview limogrgeserg = (textview)findviewbyid(r.id.limogrgeserg); final textview enderg = (textview)findviewbyid(r.id.enderg); @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.bestellung); numberpicker bn= (numberpicker) findviewbyid(r.id.biernummer); bn.setmaxvalue(100); bn.setminvalue(0); bn.setonvaluechangedlistener(new onvaluechangelistener(){ @override public void onvaluechange(numberpicker picker, int oldval, int newval) { float bp = float.valueof(bierpreis.gettext().tostring()); double zz = (double) (newval*bp); biererg.settext(f.format(zz).tostring()); enderg(); } }); .......... numberpicker lggn= (numberpicker) findviewbyid(r.id.limogrgesnummer); lggn.setmaxvalue(100); lggn.setminvalue(0); lggn.setonvaluechangedlistener(new onvaluechangelistener(){ @override public void onvaluechange(numberpicker picker, int oldval, int newval) { float lggp = float.valueof(limogrgespreis.gettext().tostring()); double zz = (double) (newval*lggp); limogrgeserg.settext(f.format(zz).tostring()); enderg(); } }); } @override public void onvaluechange(numberpicker picker, int oldval, int newval) { // todo auto-generated method stub } private void enderg() { float = float.valueof(biererg.gettext().tostring()); float b = float.valueof(seidlerg.gettext().tostring()); float c = float.valueof(weinerg.gettext().tostring()); float d = float.valueof(spritzererg.gettext().tostring()); float e = float.valueof(weinmixerg.gettext().tostring()); float g = float.valueof(colaerg.gettext().tostring()); float h = float.valueof(limoklerg.gettext().tostring()); float = float.valueof(limoklgeserg.gettext().tostring()); float j = float.valueof(limogrgeserg.gettext().tostring()); double z = (double) (a+b+c+d+e+g+h+i+j); enderg.settext(f.format(z).tostring()); } }
the logcat says there error in line 16 (as hope understood right) (final textview bierpreis....) can't find error.
so hope can me! thanks
findviewbyid
can used after onattach
called, not before, because context object indirecting using has not been yet initialized. move initialization in oncreate
after setcontentview
first use setcontentview(r.layout.bestellung);
then use
final textview bierpreis = (textview)findviewbyid(r.id.bierpreis); final textview seidlpreis = (textview)findviewbyid(r.id.seidlpreis); final textview weinpreis = (textview)findviewbyid(r.id.weinpreis); final textview spritzerpreis = (textview)findviewbyid(r.id.spritzerpreis); final textview weinmixpreis = (textview)findviewbyid(r.id.weinmixpreis); final textview colapreis = (textview)findviewbyid(r.id.colapreis); final textview limoklpreis = (textview)findviewbyid(r.id.limoklpreis); final textview limoklgespreis = (textview)findviewbyid(r.id.limoklgespreis); final textview limogrgespreis = (textview)findviewbyid(r.id.limogrgespreis); final textview biererg = (textview)findviewbyid(r.id.biererg); final textview seidlerg = (textview)findviewbyid(r.id.seidlerg); final textview weinerg = (textview)findviewbyid(r.id.weinerg); final textview spritzererg = (textview)findviewbyid(r.id.spritzererg); final textview weinmixerg = (textview)findviewbyid(r.id.weinmixerg); final textview colaerg = (textview)findviewbyid(r.id.colaerg); final textview limoklerg = (textview)findviewbyid(r.id.limoklerg); final textview limoklgeserg = (textview)findviewbyid(r.id.limoklgeserg); final textview limogrgeserg = (textview)findviewbyid(r.id.limogrgeserg); final textview enderg = (textview)findviewbyid(r.id.enderg);
Comments
Post a Comment