0

好的,所以当我运行我的应用程序时,我在 logcat 中得到了一个空指针异常,但是我查看了与我的类似的其他 StackOverflow 问题,发现我做的一切都是正确的,那么我的错误来自哪里?

这是我的主要活动:

    public double inchSnow, durationStorm, peakSnow, peakTime, finalSnow, windSpeed, snowGround, result, avgSnow;
EditText inSnow, durnStorm, pSnow, pTime, fSnow, wSpeed, sGround;

protected void onCreate(Bundle savedInstanceState) {

        final AlertDialog.Builder invalidNumber = new AlertDialog.Builder(actContext);
        invalidNumber.setTitle("Blank Field!");
        invalidNumber.setMessage("Seems like a field has been left blank, can you fix it?");
        invalidNumber.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.cancel();
            }
        });

        inSnow = (EditText) findViewById(R.id.inchesSnow);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

 getZip = (Button) findViewById(R.id.calculate);
 final Intent intent = new Intent(this, com.boreas.snowdaycalculator.result.class);

 getZip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                try{
                    inchSnow =  new Integer(inSnow.getText().toString()).intValue();
                }catch (NumberFormatException ex){
                    final AlertDialog alertDialog = invalidNumber.create();
                    invalidNumber.show();

                }

                Intent myIntent = new Intent(MainActivity.this, result.class);
                myIntent.putExtra("inchSnow",inchSnow);

                startActivity(myIntent);


            }
        });

    }

我的 MainActivity xml:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                        xmlns:tools="http://schemas.android.com/tools"
                        android:layout_width="match_parent"
                        android:layout_height="521dp"
                        android:paddingLeft="@dimen/activity_horizontal_margin"
                        android:paddingRight="@dimen/activity_horizontal_margin"
                        android:paddingTop="@dimen/activity_vertical_margin"
                        android:paddingBottom="@dimen/activity_vertical_margin"
                        tools:context=".MainActivity">


    <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/scrollView"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true">


        <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="520px"
                android:layout_alignTop="@+id/scrollView"
                android:layout_alignLeft="@+id/scrollView">

            <EditText
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:inputType="numberDecimal"
                    android:ems="10"
                    android:id="@+id/inchesSnow"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentRight="true"
                    android:hint="Inches of Snow"/>

            <EditText
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:inputType="numberDecimal"
                    android:ems="10"
                    android:id="@+id/durationStorm"
                    android:layout_below="@+id/inchesSnow"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentRight="true"
                    android:hint="Duration of Storm"/>

            <EditText
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:inputType="numberDecimal"
                    android:ems="10"
                    android:id="@+id/peakRate"
                    android:layout_below="@+id/durationStorm"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentRight="true"
                    android:hint="Peak Snow Rate"/>

            <EditText
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:inputType="numberDecimal"
                    android:ems="10"
                    android:id="@+id/peakRateTime"
                    android:layout_below="@+id/peakRate"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentRight="true"
                    android:hint="Time of Peak Snow Rate"/>

            <EditText
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:inputType="numberSigned"
                    android:ems="10"
                    android:id="@+id/finalRate"
                    android:layout_below="@+id/peakRateTime"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentRight="true"
                    android:hint="Final Snow Rate"/>

            <EditText
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:inputType="numberSigned"
                    android:ems="10"
                    android:id="@+id/windSpeed"
                    android:layout_below="@+id/finalRate"
                    android:layout_alignParentLeft="true"
                    android:hint="Wind Speed"
                    android:layout_alignParentRight="true"/>

            <EditText
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:inputType="numberDecimal"
                    android:ems="10"
                    android:id="@+id/snowGround"
                    android:layout_below="@+id/windSpeed"
                    android:layout_alignParentLeft="true"
                    android:hint="Snow Already on Ground"
                    android:layout_alignParentRight="true"/>
        </RelativeLayout>
    </ScrollView>

    <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Calculate Chances"
            android:id="@+id/calculate"
            android:layout_marginBottom="150dp"
            android:layout_alignBottom="@+id/scrollView"
            android:layout_centerHorizontal="true"/>


</RelativeLayout>

有人对我做错了什么有任何想法吗?谢谢。

4

2 回答 2

2

你需要放置

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); 

在方法中的所有其他调用之前。findViewById()将返回null,直到您设置内容视图。更重要的是,super.onCreate(savedInstanceState)将创建要使用的活动。

于 2013-08-21T03:18:21.953 回答
1

把这条线:

inSnow = (EditText) findViewById(R.id.inchesSnow);

行后:

setContentView(R.layout.activity_main);
于 2013-08-21T03:19:07.723 回答