Solved

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

Posted on 2010-09-03
12
603 Views
Last Modified: 2013-11-23
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

0
Comment
Question by:cubical38
  • 8
  • 4
12 Comments
 

Author Comment

by:cubical38
ID: 33597974
I added:

android:onClick="someMethod"

to the XML and now I get a force quit when the button is selected.
Help!
0
 

Author Comment

by:cubical38
ID: 33598102
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
 
LVL 9

Accepted Solution

by:
ioanton earned 500 total points
ID: 33598914
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
 

Author Comment

by:cubical38
ID: 33599375
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
 
LVL 9

Expert Comment

by:ioanton
ID: 33599490
Try replacing

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

with

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

Author Comment

by:cubical38
ID: 33599548
Nothing changes.  No error, no shut down, no message in the console (eclipse).  Not sure what is not being invoked or read.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 9

Expert Comment

by:ioanton
ID: 33599702
I suppose your application starts successfully and clicking the button does not fire the event. Is it true?
0
 

Author Comment

by:cubical38
ID: 33599783
That is correct.
0
 
LVL 9

Expert Comment

by:ioanton
ID: 33600219
I suggest to open the LogCat and see whether there is any exception thrown on the button click.
0
 

Author Comment

by:cubical38
ID: 33600390
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
 

Author Comment

by:cubical38
ID: 33600930
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
 

Author Closing Comment

by:cubical38
ID: 33628961
Only worked when I pulled the code out of individual activities
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
DGTEC 10.1" Quad Core Android Tablet 18 94
maven project error 5 48
MySQL  on Tomcat 8 26
Adups - Android Firmware with Preinstalled Spyware 3 9
Are you having trouble connecting or getting your iPhone / Samsung device(s) to sync with Microsoft Exchange Server?   What have you tried?   What haven't you tried?
A short article about a problem I had getting the GPS LocationListener working.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now