views:

218

answers:

2

My Activity onClick() below doesn't appear to be doing anything (not seeing any string appear), yet I dont get any errors. What am I missing? Is there a way to trace the function?

package com.HelloTabWidget2;

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

public class AlbumsActivity extends Activity {

    private Button closeButton;

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.setContentView(R.layout.tab1);
        this.closeButton = (Button)this.findViewById(R.id.button);
        this.closeButton.setOnClickListener(new OnClickListener() {
          public void onClick(View v) {
           Toast.makeText(AlbumsActivity.this, "You clicked the button", Toast.LENGTH_SHORT).show();

          }
        });


    }
}

Thanks!

+2  A: 

I believe the issue is with the anonymous method, I get an error when trying to use your code. Just add the impliments OnClickListenter.

If you have more than one button, you'll need to add a switch or something on v.getId().

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class AlbumsActivity extends Activity implements OnClickListener {
    private Button closeButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        this.closeButton = (Button)this.findViewById(R.id.button);
        this.closeButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
         Toast.makeText(this, "You clicked the button", Toast.LENGTH_SHORT).show();

    }
}
Travis
This should work. Moreover it is better for performance reasons. Registering listeners as in the question creates new objects at runtime.
Juri
Yeah but the Activity isn't what is using the onClickListener, it's the button. If you're trying to save performance by doing this you're not using OO design concepts. Please don't do this unless you have a specific reason to.
Falmarri
+1  A: 

I would say that instead of writting "...setOnClickListener(new OnClickListener() {..." i would write "...setOnClickListener(new View.OnClickListener() {..."

Tsunaze
Indeed this should solve the issue.
Juri
I have tried both methods listed above and I still receive no indication, either from my toast or Log. Is there something else I am missing?
JoshuaBen
Okay so if I set this up as an independent activity in a new project it works fine. Yet when I have it as an activity within tabs I dont see anything. Is this why it is breaking?
JoshuaBen
or some reason my project is unable to read the activities within the tab widget...
JoshuaBen
Here is my main activity:
JoshuaBen
Okay I was able to get everything working as long as it is not in individual activities... Thanks!
JoshuaBen