Android和应用程序崩溃时出现空指针异常

时间:2015-09-12 06:31:48

标签: android nullpointerexception

MainActivity.java:   这是主要的活动类。在运行它时,预期的输出是一个表,如相应的xml中所述。但是在给我第一页本身之前应用程序崩溃了。 log-cat中的错误显示为空指针异常。

 package com.codeitsuisse.team71.expensetracker;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    import android.widget.TextView;

    public class MainActivity extends Activity {
        TextView t1= (TextView) findViewById(R.id.textView1);
        TextView t3= (TextView) findViewById(R.id.textView3);
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

        public void goto_bgt(View v){
            Intent i= new Intent (MainActivity.this, Budget.class);
            //startActivityForResult(i, 1);

        }

           protected void onActivityResult(int requestCode, int resultCode, Intent data)  
           {  
                     super.onActivityResult(requestCode, resultCode, data);  
                      // check if the request code is same as what is passed  here it is 2  
                       if(requestCode==1)  
                             {  
                                double k=data.getDoubleExtra("budget",0);
                                double d= data.getDoubleExtra("goal",0);
                                t1.setText(k+"");
                                t3.setText(d+"");
                             }  
         }  

    }

activity_main.xml中:

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="1"
    android:id="@+id/linearLayout">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="WELCOME USER!"
        android:id="@+id/textView"
        android:layout_gravity="center_horizontal" />

    <TableLayout android:id="@+id/tabla_cuerpo"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_marginTop="200dp"
        android:background="#FF0000">
        <TableRow android:id="@+id/tableRow1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView android:textColor="#000"
                android:textStyle="bold"
                android:gravity="center_horizontal"
                android:background="#f00" android:layout_margin="1dip"
                android:id="@+id/textViewb" android:layout_weight="0.3"
                android:layout_width="100dp" android:layout_height="50dp"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="BUDGET"/>
            <TextView android:textColor="#000"
                android:textStyle="bold"
                android:gravity="center_horizontal"
                android:background="#f00" android:layout_margin="1dip"
                android:id="@+id/textViewe" android:layout_weight="0.3"
                android:layout_width="100dp" android:layout_height="50dp"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="EXPENSES"/>

            <TextView android:textColor="#000"
                android:textStyle="bold"
                android:gravity="center_horizontal"
                android:background="#f00" android:layout_margin="1dip"
                android:id="@+id/textViewg" android:layout_weight="0.3"
                android:layout_width="100dp" android:layout_height="50dp"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="SAVINGS GOAL"/>
        </TableRow>
        <TableRow android:id="@+id/tableRow1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView android:background="#000000"
                android:textColor="#FFFFFF"
                android:layout_margin="1dip"
                android:id="@+id/textView1"
                android:layout_weight="0.3"
                android:layout_width="wrap_content"
                android:layout_height="40dp"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Celda1"/>
            <TextView android:background="#000000"
                android:textColor="#FFFFFF"
                android:layout_margin="1dip"
                android:id="@+id/textView2"
                android:layout_weight="0.3"
                android:layout_width="wrap_content"
                android:layout_height="40dp"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Celda2"/>
            <TextView android:background="#000000"
                android:layout_margin="1dip"
                android:textColor="#FFFFFF"
                android:id="@+id/textView3"
                android:layout_weight="0.3"
                android:layout_width="wrap_content"
                android:layout_height="40dp"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Celda3"/>
        </TableRow>
    </TableLayout>


</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/linearLayout"
    android:layout_marginTop="27dp"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/linearLayout2"
        android:text="update budget"
        android:onClick="goto_bgt"
         />

    <Button
        android:id="@+id/button2"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="update exp" />
</LinearLayout>

我的日志是

> (MainActivity.java:11) 09-12 12:08:25.358: E/AndroidRuntime(1504): at
> java.lang.reflect.Constructor.newInstance(Native Method) 09-12
> 12:08:25.358: E/AndroidRuntime(1504): at
> java.lang.Class.newInstance(Class.java:1572) 09-12 12:08:25.358:
> E/AndroidRuntime(1504): at
> android.app.Instrumentation.newActivity(Instrumentation.java:1088)
> 09-12 12:08:25.358: E/AndroidRuntime(1504): at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
> 09-12 12:08:25.358: E/AndroidRuntime(1504): ... 10 more -->

3 个答案:

答案 0 :(得分:0)

activity_main.xml在结尾处有一个结束</RelativeLayout> - 但是没有相同标记的相关开放。

答案 1 :(得分:0)

setContentView(R.layout.activity_main)

之后使用此功能
TextView t1= (TextView) findViewById(R.id.textView1);
TextView t3= (TextView) findViewById(R.id.textView3);

TextView t1,t3;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         t1= (TextView) findViewById(R.id.textView1);
         t3= (TextView) findViewById(R.id.textView3);
        ......
    }
}

答案 2 :(得分:0)

您无法以您的方式声明TextView

这样做

TextView t1,t3;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        t1= (TextView) findViewById(R.id.textView1);
        t3= (TextView) findViewById(R.id.textView3);
        ......
    }
}