开发者

java.lang.NullPointerException when trying to set EditText widget

I am getting a java.lang.NullPointerException error on line 41 (setText) when I execute the following code.

I have seen EditText boxes being set with this line before and I do not understand what I am doing wrong.

I have no experience with eclipse or java but I am trying to learn. Please help!

Code:

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"
andro开发者_StackOverflow社区id: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


You haven't assigned to barometricPressure - it is still null.

For altitude you have

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

Presumably you need something similar for barometricPressure too.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜