Solved

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

Posted on 2010-09-03
12
595 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

The case of the missing phone talks about the way a small electronic gadget (the mobile phone) has penetrated into our lives and has made us addicted to it.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

743 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

11 Experts available now in Live!

Get 1:1 Help Now