简体   繁体   中英

Android NullPointer Exception in Activity's onCreate

I have a simple application to multiply two numbers from text boxes and display result in third box. There is no any syntax errors in the code but when I am running an application i get this error: application has stopped unexpectedly.

Here is the java code:

package c.example.rectangle;

import android.os.Bundle;

import android.view.View;
import android.app.Activity;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener{

    EditText l = (EditText) findViewById(R.id.length);
    EditText w = (EditText) findViewById(R.id.width);
    TextView a = (TextView) findViewById(R.id.lblarea);
    Button b = (Button) findViewById(R.id.calculate);


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);  
        b.setOnClickListener(this);

    }

    public void onClick(View v) {
        calculateRectangle(l.getText().toString(), w.getText().toString());


    }
    private void calculateRectangle(String clength, String cwidth){


        int area = Integer.parseInt(clength)*Integer.parseInt(cwidth);

        b.setText(String.valueOf(area)); 
}}

And here is my XML file.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#8B4513"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/label1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"
        android:background="#2F4F4F"
        android:gravity="center"
        android:text="@string/rect"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#8B4513"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/label2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="50dp"
            android:background="#2F4F4F"
            android:gravity="center"
            android:text="@string/cm"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/length"
            android:layout_width="110dp"
            android:layout_height="21dp"
            android:layout_marginLeft="40dp"
            android:layout_marginTop="50dp"
            android:background="#2F4F4F"
            android:ems="10"
            android:gravity="center"
            android:inputType="number" />


    </LinearLayout>

 <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#8B4513"
        android:orientation="horizontal" >

    <TextView
        android:id="@+id/label3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#2F4F4F"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="20dp"
        android:text="@string/breadth"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/width"
        android:layout_width="110dp"
        android:layout_height="21dp"
        android:layout_marginLeft="33dp"
        android:layout_marginTop="20dp"   
        android:background="#2F4F4F"
        android:inputType="number"
        android:ems="10" 
        android:gravity="center"
        >

        <requestFocus />
    </EditText>

</LinearLayout>

 <Button
     android:id="@+id/calculate"
     android:layout_width="fill_parent"
     android:layout_marginLeft="100dip"
     android:layout_marginRight="100dip"
     android:layout_height="wrap_content"
     android:text="@string/calculate"
     android:layout_marginTop="20dp" />

 <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#8B4513"
        android:orientation="horizontal" >

 <TextView
     android:id="@+id/label4"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_marginLeft="30dp"
     android:layout_marginTop="20dp"
     android:background="#2F4F4F"
     android:text="@string/area"
     android:textAppearance="?android:attr/textAppearanceMedium" />

 <TextView
     android:id="@+id/lblarea"
     android:layout_width="110dp"
     android:layout_height="21dp"
     android:layout_marginLeft="60dp"
     android:layout_marginTop="20dp"
     android:background="#2F4F4F"
     android:gravity="center"/>
 </LinearLayout>
  </LinearLayout>

Please help.

I know, that you got correct answer, but I just want to explain you why you need to write code as @Mr.Me says.

You describe your View elements in start of class, and trying to initialize them there. It is not correct. Because you have not attached layout file to activity at the moment when constructor will run your initialization of Views objects. As you can see, you are using findViewById() method, but before use it you should call setContentView().

For better understanding, read Activity Lifecycle , pay attention to rendering proccess.

Rearrange your code to look like this:

 EditText l;
 EditText w;
 TextView a;
 Button b;

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); 
    l = (EditText) findViewById(R.id.length);
    w = (EditText) findViewById(R.id.width);
    a = (TextView) findViewById(R.id.lblarea);
    b = (Button) findViewById(R.id.calculate);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM