0

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.

1
  • I am new to Android and I am not sure how to do it. I am in logcat but there is many lines which i am unable to copy and paste. some of them are red. Please help. Commented Feb 6, 2013 at 21:06

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Also The activity's context would not be ready , trying to access layout inflater before onCreate never worked for me
Yep, Activity is not allowing operations with its views elements, before onCreate() is not finished
0

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);

6 Comments

Now application turns on but does not display result of calculation in TextView "lblarea" :(
That is because you are setting the text to the button. Use a.setText instead of b.setText
ok I got it. Just restarted eclipse and it worked. Thank You.
it said i need 15 points of reputation to upvote the answer. sorry.
i am new to this forum so dont know the rules exactly but i think i just upvoted it for you?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.