1

Hello i have a problem with running the application, when ill run it, its opening but when i will press the button saying me that the application is stopped and cant run: “Unfortunately, App has Stopped”. Here is my simple code and i hope i will get the answer fast... Thanks allot :)

package com.stefan.stefan1;

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

public class MainActivity extends Activity {

    TextView textView1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Button button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                textView1.setText("Hello Im Stefan !");
            }
        });

    }

    @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;
    }

}

Hello again i have new problem i have added another label(textView2) and im trying to add again this line textView2 = (TextView) findViewById(R.id.textView2); but giving me error for adding something in class "R.java" please help .. :/

Stefan Rafa
  • 27
  • 1
  • 9

1 Answers1

1

You forgot to initialize textView1.

So when you're doing :

textView1.setText("Hello Im Stefan !");

it throws a NullPointerException and stop your app.

If you defined your textview in the layout, add after setContentView

textView1 = (TextView) findViewById(R.id.idOfYourTextView);

P.S : You should always provide the stacktrace(logcat) when you have such an error.

Alexis C.
  • 91,686
  • 21
  • 171
  • 177
  • oh thanks allot but just one more question why is giving me 3 errors: `[2013-07-02 14:11:43 - ddms] Can't bind to local 8700 for debugger [2013-07-02 14:11:50 - ddms] Can't bind to local 8700 for debugger [2013-07-02 14:14:14 - ddms] Can't bind to local 8700 for debugger` – Stefan Rafa Jul 02 '13 at 12:25
  • Check [this](http://stackoverflow.com/questions/3318738/eclipse-ddms-error-cant-bind-to-local-8600-for-debugger) and [this](http://stackoverflow.com/questions/13198646/cant-bind-to-local-xxxx-for-debugger) – Alexis C. Jul 02 '13 at 12:27
  • and btw the application is running smoothly THANKS ! :D – Stefan Rafa Jul 02 '13 at 12:28
  • hello i have now again a problem i have added another label(textView2) and i trying to add again this line `textView2 = (TextView) findViewById(R.id.textView2);` but giving me error for adding something in class "R.java" please help .. :/ – Stefan Rafa Jul 02 '13 at 13:45
  • @StefanRafa did u declare the field 'TextView textView2;' ? – Karan Sharma Nov 02 '13 at 18:33