执行以下代码时,我在第41行(setText)上遇到java.lang.NullPointerException错误。
我之前看到过使用此行设置的EditText框,我不明白我做错了什么。
我没有使用eclipse或java的经验,但我正在努力学习。请帮忙!
代码:
package Psychrometric.Calculator;
import android.app.Activity;
import android.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.text.TextUtils;
import android.os.Bundle;
public class Calculator extends Activity {
private EditText altitude;
private EditText barometricPressure;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button calcButton = (Button) findViewById(R.id.calcButton);
calcButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
altitude = (EditText) findViewById(R.id.altitude);
if (altitude.getText().length() != 0)
{
double altitudeDouble = new Double(altitude.getText().toString());
double barometricPressureDouble = 29.92 * java.lang.Math.pow((1 - 0.0000068753 * altitudeDouble), 5.259);
altitude.setText(Double.toString(altitudeDouble));
barometricPressure.setText(Double.toString(barometricPressureDouble));
}
}
});
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TableLayout
android:id="@+id/widget27"
android:layout_width="320px"
android:layout_height="825px"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:stretchColumns="1"
>
<TableRow
android:id="@+id/firstRow"
android:layout_width="fill_parent"
android:layout_height="75px"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
>
<Button
android:id="@+id/calcButton"
android:layout_width="185px"
android:layout_height="75px"
android:text="Calculate"
>
</Button>
</TableRow>
<TableRow
android:id="@+id/widget45"
android:layout_width="fill_parent"
android:layout_height="50px"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
>
<TextView
android:id="@+id/widget221"
android:layout_width="wrap_content"
android:layout_height="50px"
android:text="Altitude"
>
</TextView>
<EditText
android:id="@+id/altitude"
android:layout_width="wrap_content"
android:layout_height="50px"
android:textSize="18sp"
android:numeric="decimal"
>
</EditText>
<TextView
android:id="@+id/widget454"
android:layout_width="wrap_content"
android:layout_height="50px"
android:text="ft"
>
</TextView>
</TableRow>
<TableRow
android:id="@+id/widget32"
android:layout_width="fill_parent"
android:layout_height="50px"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
>
<TextView
android:id="@+id/widget222"
android:layout_width="wrap_content"
android:layout_height="50px"
android:text="Barometric Pressure"
>
</TextView>
<EditText
android:id="@+id/barometricPressure"
android:layout_width="wrap_content"
android:layout_height="50px"
android:textSize="18sp"
android:numeric="decimal"
>
</EditText>
<TextView
android:id="@+id/widget455"
android:layout_width="wrap_content"
android:layout_height="50px"
android:text="in. Hg"
>
</TextView>
</TableRow>
</TableLayout>
</ScrollView>
LogCat:
02-11 12:49:02.334: DEBUG/AndroidRuntime(11161): Shutting down VM
02-11 12:49:02.334: WARN/dalvikvm(11161): threadid=1: thread exiting with uncaught exception (group=0x400259f8)
02-11 12:49:02.394: ERROR/AndroidRuntime(11161): FATAL EXCEPTION: main
02-11 12:49:02.394: ERROR/AndroidRuntime(11161): java.lang.NullPointerException
02-11 12:49:02.394: ERROR/AndroidRuntime(11161): at Psychrometric.Calculator.Calculator$1.onClick(Calculator.java:41)
02-11 12:49:02.394: ERROR/AndroidRuntime(11161): at android.view.View.performClick(View.java:2408)
02-11 12:49:02.394: ERROR/AndroidRuntime(11161): at android.view.View$PerformClick.run(View.java:8817)
02-11 12:49:02.394: ERROR/AndroidRuntime(11161): at android.os.Handler.handleCallback(Handler.java:587)
02-11 12:49:02.394: ERROR/AndroidRuntime(11161): at android.os.Handler.dispatchMessage(Handler.java:92)
02-11 12:49:02.394: ERROR/AndroidRuntime(11161): at android.os.Looper.loop(Looper.java:144)
02-11 12:49:02.394: ERROR/AndroidRuntime(11161): at android.app.ActivityThread.main(ActivityThread.java:4937)
02-11 12:49:02.394: ERROR/AndroidRuntime(11161): at java.lang.reflect.Method.invokeNative(Native Method)
02-11 12:49:02.394: ERROR/AndroidRuntime(11161): at java.lang.reflect.Method.invoke(Method.java:521)
02-11 12:49:02.394: ERROR/AndroidRuntime(11161): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-11 12:49:02.394: ERROR/AndroidRuntime(11161): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-11 12:49:02.394: ERROR/AndroidRuntime(11161): at dalvik.system.NativeStart.main(Native Method)
02-11 12:49:02.404: WARN/ActivityManager(220): Force finishing activity Psychrometric.Calculator/.Calculator
答案 0 :(得分:5)
您尚未分配到barometricPressure
- 它仍为空。
对于altitude
你有
altitude = (EditText) findViewById(R.id.altitude);
大概你也需要barometricPressure
类似的东西。