android: button onClick(), cant tell if anything is happening...

With the following code block below, I get no response.  Im not sure if the function is not firing and or if I am missing something.  New to Android and Java but have lots of OOP.  Anyone have any suggestions?

Thanks!
//JAVA:
package com.HelloTabWidget2;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.util.Log;
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) {
        	  Log.d("button was clicked", null);
        	  Toast.makeText(AlbumsActivity.this, "You clicked the button", Toast.LENGTH_SHORT).show();

          }
        });


    }
}

//XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:id="@+id/tab1Layout"
    android:orientation="vertical"
    android:gravity="center">
	    <TextView
	        android:id="@+id/label"
	        android:layout_width="fill_parent"
	        android:layout_height="wrap_content"
	        android:text=""
	        />
	    <EditText
	        android:id="@+id/entry"
	        android:layout_width="250px"
	        android:layout_height="wrap_content"
	        android:background="@android:drawable/editbox_background"
	        android:layout_below="@id/label"/>
	    <Button
	        android:id="@+id/button"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:layout_below="@id/entry"
	        android:layout_alignParentRight="true"
	        android:layout_marginLeft="10dip"
	        android:text="OK" />
	  
</LinearLayout>

Open in new window

cubical38Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
ioantonConnect With a Mentor Commented:
Still not working? I can't see any mistake in the code above; however, if I include the line

Log.d("button was clicked", null);

in the code, the application crashes. Replacing the second argument with a valid string solves the problem.
0
 
cubical38Author Commented:
I added:

android:onClick="someMethod"

to the XML and now I get a force quit when the button is selected.
Help!
0
 
cubical38Author Commented:
Still no result:
package com.HelloTabWidget2;

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

public class AlbumsActivity extends Activity {


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

        this.setContentView(R.layout.tab1);
        final Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // Perform action on clicks
                Toast.makeText(AlbumsActivity.this, "Something", Toast.LENGTH_SHORT).show();
            }
        });


    }
}

Open in new window

0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
cubical38Author Commented:
Okay, I have (code below):

What would be a valid string?  Nothing gets broken, yet there is no indication that the onClick() has fired.  Is there something missing in my xml (below)?

Thanks for the reply, I feel like there is something very small being overlooked.
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.setContentView(R.layout.tab1);
        final Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	Log.d("button was clicked", null);
                Toast.makeText(ArtistsActivity.this, "Something", Toast.LENGTH_SHORT).show();
            }
        });


    }


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:id="@+id/tab1Layout"
    android:orientation="vertical"
    android:gravity="center">
	    <TextView
	        android:id="@+id/label"
	        android:layout_width="fill_parent"
	        android:layout_height="wrap_content"
	        android:text=""
	        />
	    <EditText
	        android:id="@+id/entry"
	        android:layout_width="250px"
	        android:layout_height="wrap_content"
	        android:background="@android:drawable/editbox_background"
	        android:layout_below="@id/label"/>
	    <Button android:id="@+id/button"
		    android:layout_width="50px"
		    android:layout_height="wrap_content"
		    android:layout_alignParentBottom="true"
		    android:text="button" />
	  
</LinearLayout>

Open in new window

0
 
ioantonCommented:
Try replacing

Log.d("button was clicked", null);

with

Log.d(null, "button was clicked");
0
 
cubical38Author Commented:
Nothing changes.  No error, no shut down, no message in the console (eclipse).  Not sure what is not being invoked or read.
0
 
ioantonCommented:
I suppose your application starts successfully and clicking the button does not fire the event. Is it true?
0
 
cubical38Author Commented:
That is correct.
0
 
ioantonCommented:
I suggest to open the LogCat and see whether there is any exception thrown on the button click.
0
 
cubical38Author Commented:
There is still no indication of a button being pressed, clicked, nothing.  The button lights up green as it should but I see nothing.  Where should I see the string?  If it is through a toast I should see it pop up on the screen i assume.
0
 
cubical38Author Commented:
Could it have something to do with the fact that these are being add to a tab widget?  Here is my main Activity class:


//MAIN:

package com.HelloTabWidget2;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.Toast;


public class HelloTabWidget2 extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab

        // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, ArtistsActivity.class);

        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("artists").setIndicator("Search",
                          res.getDrawable(R.drawable.ic_tab_artists))
                      .setContent(R.id.tab1Layout);
        
        tabHost.addTab(spec);

        // Do the same for the other tabs
        intent = new Intent().setClass(this, AlbumsActivity.class);
        spec = tabHost.newTabSpec("albums").setIndicator("Events",
                          res.getDrawable(R.drawable.ic_tab_artists))
                      .setContent(R.id.tab2Layout);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, SongsActivity.class);
        spec = tabHost.newTabSpec("songs").setIndicator("News",
                          res.getDrawable(R.drawable.ic_tab_artists))
                      .setContent(intent);
        tabHost.addTab(spec);
        
        intent = new Intent().setClass(this, WhoActivity.class);
        spec = tabHost.newTabSpec("songs").setIndicator("gold circle",
                          res.getDrawable(R.drawable.ic_tab_artists))
                      .setContent(intent);
        tabHost.addTab(spec);
        
        intent = new Intent().setClass(this, MineActivity.class);
        spec = tabHost.newTabSpec("songs").setIndicator("about",
                          res.getDrawable(R.drawable.ic_tab_artists))
                      .setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(2);
       
        //final Button button = (Button) findViewById(R.id.button);
        //button.setOnClickListener(new OnClickListener() {
            //public void onClick(View v) {
                // Perform action on clicks
                //Toast.makeText(null, "Beep Bop", Toast.LENGTH_SHORT).show();
            //}
        //});
        
    }

}

//

Open in new window

0
 
cubical38Author Commented:
Only worked when I pulled the code out of individual activities
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.