user1981245
user1981245

Reputation: 121

I'm getting "Unfortunately Myapp has stopped working" when i launch the android project in the emulator

Iam a beginner in android project development. I have started developing a basic app using a tutorial i found online. Im using eclipse.There were no errors while running my project. Im getting this error immediately after i click the app on my emulator. Following r d xml and java coding i used:

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

<TextView
    android:id="@+id/tvDisplay"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="@string/result"
    android:textSize="30sp" />

<Button
    android:id="@+id/a"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="@string/add"
    android:textSize="20sp" />

<Button
    android:id="@+id/s"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="@string/sub"
    android:textSize="20sp" /></LinearLayout>

Java:

package com.app.myapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

int count;
Button add,sub;
TextView disp;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.linear);
    count=0;
    add=(Button) findViewById(R.id.a);
    sub=(Button) findViewById(R.id.s);
    disp=(Button) findViewById(R.id.tvDisplay);
    add.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            count=count+1;
            disp.setText("Result is " + count);

        }
    });
    sub.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            count=count-1;
            disp.setText("Result is " + count);
        }
    });
}
}

Kindly suggest solutions after which I should proceed my project development.

Upvotes: 0

Views: 621

Answers (1)

wtsang02
wtsang02

Reputation: 18873

TextView disp;
disp=(Button) findViewById(R.id.tvDisplay);

You have declared 'disp' as a TextView; But in the code you cast it to a Button. Change the cast to TextView like this :

disp=(TextView) findViewById(R.id.tvDisplay);

Upvotes: 1

Related Questions