SaKer
SaKer

Reputation: 107

setOnClickListener causes a crash of an APP

Hello I have this very simple code I'm trying to run via Android Studio

public class MainActivity extends ActionBarActivity {
Button random;
TextView display;

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

    random = (Button) findViewById(R.id.button);
    display = (TextView) findViewById(R.id.TextView);

    random.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            display.setText("I have changed");
        }
    });

I haven't really added very much, but whenever I use the setOnClickListener no matter what's inside of it crashes the app. I couldn't find a solution for this.

Thank you.

//edit: sorry. I added a wrong code, random is a button

Upvotes: 2

Views: 6024

Answers (5)

Marco
Marco

Reputation: 1493

It doesn't seem to be your case but I had the same problem and it was caused by having these lines

mButton = findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
    // ...
}
});

before

setContentView(R.layout.activity_main);

Upvotes: 3

Eshin Sergey
Eshin Sergey

Reputation: 11

Firstly,check logcat by error level Secondly,random is not initialized!!

You need

1.Create button in activity_main.xml

 For example, 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity" >
<Button
    android:id="@+id/myCoolButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />
 </RelativeLayout>

2.Init that button in code

Button myCoolButton = (Button) findViewById(R.id.myCoolButton);

3.Attach listener to button

myCoolButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
    display.setText("I have changed");
}});

And it works! Hope that help you

Upvotes: 0

Vaibhav Agarwal
Vaibhav Agarwal

Reputation: 4499

In your code  you are setting click listener on a button. Please make sure you get right id and right object to set listener. The button you want to set clcik listener on should have same id to the id you defined in you activity 

In Activity
Button button =(Button)findViewById(R.id.button1);

In XML
 <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

Upvotes: 0

Denis Shurygin
Denis Shurygin

Reputation: 188

What is "random"? I think correctly will be:

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        display.setText("I have changed");
    }
});

Upvotes: 0

Emil Adz
Emil Adz

Reputation: 41099

You are setting the setOnClickListener method on the random object which by your code is currently Null.

So you are getting a NullPointerException.

I think you intended it to be button instead.

Upvotes: 0

Related Questions