声明了EditText,LogCat仍然提供nullpointerexception

时间:2013-08-21 03:14:37

标签: java android nullpointerexception android-edittext logcat

好的,所以当我运行我的应用程序时,我在logcat中得到了nullpointerexception,但是我查看了与我类似的其他StackOverflow问题并发现我已经做好了一切,所以我的错误来自哪里? / p>

这是我的MainActivity:

    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>

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

2 个答案:

答案 0 :(得分:2)

你需要放置

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

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

答案 1 :(得分:1)

放线:

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

行后:

setContentView(R.layout.activity_main);