日食NullPointerRxception日食、NullPointerRxception

2023-09-04 07:36:44 作者:我们不要再相爱了

任何人都可以请帮助我,我没有找到我做了什么错在我的活动

Can anyone pls help me: i don't find what I've done wrong in my activity.

这是从logcat的错误:

This is the error from the 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 to instantiate activity ComponentInfo{pack.coderzheaven/pack.coderzheaven.Bestellung}: java.lang.NullPointerException
11-27 10:37:25.551: E/AndroidRuntime(26862):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
11-27 10:37:25.551: E/AndroidRuntime(26862):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
11-27 10:37:25.551: E/AndroidRuntime(26862):    at android.app.ActivityThread.access$900(ActivityThread.java:161)
11-27 10:37:25.551: E/AndroidRuntime(26862):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
11-27 10:37:25.551: E/AndroidRuntime(26862):    at android.os.Handler.dispatchMessage(Handler.java:102)
11-27 10:37:25.551: E/AndroidRuntime(26862):    at android.os.Looper.loop(Looper.java:157)
11-27 10:37:25.551: E/AndroidRuntime(26862):    at android.app.ActivityThread.main(ActivityThread.java:5356)
11-27 10:37:25.551: E/AndroidRuntime(26862):    at java.lang.reflect.Method.invokeNative(Native Method)
11-27 10:37:25.551: E/AndroidRuntime(26862):    at java.lang.reflect.Method.invoke(Method.java:515)
11-27 10:37:25.551: E/AndroidRuntime(26862):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
11-27 10:37:25.551: E/AndroidRuntime(26862):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
11-27 10:37:25.551: E/AndroidRuntime(26862):    at 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):    at android.app.Activity.findViewById(Activity.java:1965)
11-27 10:37:25.551: E/AndroidRuntime(26862):    at pack.coderzheaven.Bestellung.<init>(Bestellung.java:16)
11-27 10:37:25.551: E/AndroidRuntime(26862):    at java.lang.Class.newInstanceImpl(Native Method)
11-27 10:37:25.551: E/AndroidRuntime(26862):    at java.lang.Class.newInstance(Class.java:1208)
11-27 10:37:25.551: E/AndroidRuntime(26862):    at android.app.Instrumentation.newActivity(Instrumentation.java:1079)
11-27 10:37:25.551: E/AndroidRuntime(26862):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2222)
11-27 10:37:25.551: E/AndroidRuntime(26862):    ... 11 more

和java的文件:

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 a = 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 i = 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());     
}
}

在logcat中说,有在第16行错误(我希望我的理解是正确的)(最后的TextView比尔preIS ....),但我找不到任何错误。

the logcat says that there is an error in line 16 (as I hope I understood it right) (final textview Bierpreis....) but i can't find any error.

所以,我希望你能帮助我!谢谢

So I hope you can help me! Thanks

推荐答案

findViewById 可以在 onAttach 中使用的叫,而不是之前,因为使用您indirecting上下文对象还没有被初始化。移动的onCreate 所有的初始化后的setContentView

findViewById can be used after onAttach is called, not before, because the context object you are indirecting using has not been yet initialized. Move all the initialization in onCreate after setContentView

第一次使用的setContentView(R.layout.bestellung);

然后用

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);