Unfotunately, app has stopped

Unfotunately, app has stopped

package com.example.qao;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Main extends ActionBarActivity {
     int counter; 
     Button add, sub;
     TextView display;
    protected void onCreate(Bundle savedInstanceState) {
        add= (Button)findViewById(R.id.button1);
        sub= (Button)findViewById(R.id.button2);
        add.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // TODO Auto-generated method stub
                display.setText("Your Total Is "+ counter);
        } );
        sub.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // TODO Auto-generated method stub
                display.setText("Your Total Is " +counter);


this is my program it shows no error on eclipse but when i run the program emulator doesn't run it emulator shows a message "unfortunately app has stopped" someone please find the mistake in it. I just started learning android development but now i am stuck with that problem. 

6 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.


Could you add log information? You can find it using the Eclipse with logcat or, before run the app while the emulator is running, type in the terminal:
$> adb shell logcat

Copy and paste the error message and the exception stack information in the forum, so it can be easier to debug.

Looks like a NullPointerException somewhere in your code, the log information will help you.

If you don't know how to get the logcat output you can also simply zip your full project and attach it to a message. 

The code you attached looks correct. So I guess that add or sub is null because you Changed something in the layout file. 

If you app is stopped as soon as it is started, then the problem is likely in the code executed inside OnCreate().

Excluding code inside listeners, because it is not executed, there are just two rows of code that should cause a forced stop for NullPointerException: the two rows where you set listener for add and sub buttons. My guess is that IDs of at least one Button is not button1 or button2 (check you XML layout).

Thanks all of you!

I really appreciate your help. I just started learning android development.

here it takes too long to get a reply. If you can give me your email. i can share my problem with you on your email address directly.

here is mine farhanijaz03@gmail.com if you want to give your email address just send me a mail.

thanks for your help

To solve this kind of problems is very important to post the messages displayed in LogCat, otherwise is very difficult to get a solution :(.

for my experience if you have the message: ""unfortunately app has stopped" the two main causes are:

1) any of this elements:

        add= (Button)findViewById(R.id.button1);
        sub= (Button)findViewById(R.id.button2);

doesn´t exist inside the layout: "activity_main.xml"

2)  probably your activity is not registered inside the "AndroidManifest.xml".



Leave a Comment

Please sign in to add a comment. Not a member? Join today