有些人可以看看下面的内容,并尝试解释我所缺少的吗?我之前在应用程序中使用了以下方法,它的运行效果非常好。我肯定我错过了一些愚蠢的东西,很想让别人看看...

输入空气量和压力,然后单击RadioButton ForwardCBD。这会产生双重功效,然后应将其输出到屏幕上,但会导致应用崩溃。

有任何想法吗?

EditText AirVolume, Pressure,FCBD,BCBD,BCDD;
TextView Power;
RadioGroup Fans;
RadioButton ForwardCBD, BackwardCBD, BackwardCDD;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.car);

    AirVolume=(EditText)findViewById(R.id.AirVolume);
    Pressure=(EditText)findViewById(R.id.Pressure);

    Power=(TextView)findViewById(R.id.Power);

    ForwardCBD=(RadioButton)findViewById(R.id.ForwardCBD);
    BackwardCBD=(RadioButton)findViewById(R.id.BackwardCBD);
    BackwardCDD=(RadioButton)findViewById(R.id.BackwardCDD);

    Fans=(RadioGroup)findViewById(R.id.Fans);
    Fans.setOnCheckedChangeListener(this);

}

public void onCheckedChanged(RadioGroup Fans, int aplication) {

    if (aplication==R.id.ForwardCBD){

        double airVolume = Double.parseDouble(AirVolume.getText().toString());
        double pressure = Double.parseDouble(Pressure.getText().toString());
        double fCBD = Double.parseDouble(FCBD.getText().toString());
        double power = (airVolume * pressure) / (fCBD /100);
        DecimalFormat df = new DecimalFormat();
        Power.setText(df.format(power)+("W"));

    }
    if (aplication==R.id.BackwardCBD){

        double airVolume = Double.parseDouble(AirVolume.getText().toString());
        double pressure = Double.parseDouble(Pressure.getText().toString());
        double bCBD = Double.parseDouble(BCBD.getText().toString());
        double power = (airVolume * pressure) / (bCBD /100);
        DecimalFormat df = new DecimalFormat();
        Power.setText(df.format(power)+("W"));

    }
    if (aplication==R.id.BackwardCDD){
        double airVolume = Double.parseDouble(AirVolume.getText().toString());
        double pressure = Double.parseDouble(Pressure.getText().toString());
        double bCDD = Double.parseDouble(BCDD.getText().toString());
        double power = (airVolume * pressure) / (bCDD /100);
        DecimalFormat df = new DecimalFormat();
        Power.setText(df.format(power)+("W"));
    }

}
}


请参阅下面的LogCat。

不太确定如何在此处以体面的格式呈现它,因此请提前对不起!

08-19 12:10:24.576: E/AndroidRuntime(11733): FATAL EXCEPTION: main
08-19 12:10:24.576: E/AndroidRuntime(11733): java.lang.NullPointerException
08-19 12:10:24.576: E/AndroidRuntime(11733):    at com.test.will.CarSystems.onCheckedChanged(CarSystems.java:46)
08-19 12:10:24.576: E/AndroidRuntime(11733):    at android.widget.RadioGroup.setCheckedId(RadioGroup.java:172)
08-19 12:10:24.576: E/AndroidRuntime(11733):    at android.widget.RadioGroup.access$600(RadioGroup.java:52)
08-19 12:10:24.576: E/AndroidRuntime(11733):    at android.widget.RadioGroup$CheckedStateTracker.onCheckedChanged(RadioGroup.java:342)
08-19 12:10:24.576: E/AndroidRuntime(11733):    at android.widget.CompoundButton.setChecked(CompoundButton.java:128)
08-19 12:10:24.576: E/AndroidRuntime(11733):    at android.widget.CompoundButton.toggle(CompoundButton.java:87)
08-19 12:10:24.576: E/AndroidRuntime(11733):    at android.widget.RadioButton.toggle(RadioButton.java:75)
08-19 12:10:24.576: E/AndroidRuntime(11733):    at android.widget.CompoundButton.performClick(CompoundButton.java:99)
08-19 12:10:24.576: E/AndroidRuntime(11733):    at android.view.View$PerformClick.run(View.java:14400)
08-19 12:10:24.576: E/AndroidRuntime(11733):    at android.os.Handler.handleCallback(Handler.java:605)
08-19 12:10:24.576: E/AndroidRuntime(11733):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-19 12:10:24.576: E/AndroidRuntime(11733):    at android.os.Looper.loop(Looper.java:154)
08-19 12:10:24.576: E/AndroidRuntime(11733):    at android.app.ActivityThread.main(ActivityThread.java:4945)
08-19 12:10:24.576: E/AndroidRuntime(11733):    at java.lang.reflect.Method.invokeNative(Native Method)
08-19 12:10:24.576: E/AndroidRuntime(11733):    at java.lang.reflect.Method.invoke(Method.java:511)
08-19 12:10:24.576: E/AndroidRuntime(11733):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-19 12:10:24.576: E/AndroidRuntime(11733):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-19 12:10:24.576: E/AndroidRuntime(11733):    at dalvik.system.NativeStart.main(Native Method)

最佳答案

您在哪里将组件分配给FCBD,BCBD,BCDD;可能是空指针。

你的电话

double fCBD = Double.parseDouble(FCBD.getText().toString());


但是您在哪里定义/分配了FCBD,即

FCBD=(EditText)findViewById(R.id.FCBD);


否则,请进入调试模式,然后发现其中一个属性为null,这是您尝试调用方法的原因。

08-18 03:20