有些人可以看看下面的内容并尝试解释我错过的内容吗?我之前在应用程序中使用了以下方法,并且它工作得很好。我确定我错过了一些愚蠢的东西,并希望有人来看看它......
我输入风量和压力,然后点击我的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)
答案 0 :(得分:1)
您在哪里为FCBD,BCBD,BCDD分配了组件;这可能是空指针。
你的来电
double fCBD = Double.parseDouble(FCBD.getText().toString());
但你在哪里定义/分配了FCBD,即
FCBD=(EditText)findViewById(R.id.FCBD);
否则,在调试模式中一步一步,你会发现你的一个属性是null,你尝试调用一个方法。