Onclick event does not fire for a button

Common bugs/problems with the Android SDK the Emulator and the ADT-Plugin.

Onclick event does not fire for a button

Postby adolfchacko » Sat Jul 02, 2011 11:08 am

Hi,

OnClickListener is implemented in the Activity. onClick method is overridden. And I have added the onClickListener on the button. However when I click on the button, the control does not go to the onClick method.

I am not getting any clue about it.

Do we need some permission in the manifest to click a button?
Any special settings required in the AVD?
Any particular SDK to be used..?


Can you please help.

Thanks
Chacko


import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class UserHome extends Activity implements OnClickListener{
/** Called when the activity is first created. */


public Button expBtnServer;
public Button expBtnXML;
public Button close;

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

this.expBtnServer = (Button)this.findViewById(R.id.expBtnServer);
this.expBtnServer.setFocusable(true);
this.expBtnServer.setClickable(true);
this.expBtnServer.setOnClickListener(this);
//expBtnXML =(Button)this.findViewById(R.id.exprtBtnXML);
//expBtnXML.setOnClickListener(this);
et.setText(contactDetails.toString());

setContentView(R.layout.main);

Toast pieceToast = Toast.makeText(getApplicationContext(), "On Create", Toast.LENGTH_LONG);
pieceToast.show();
}

public void onClick(View button)
{

Toast pieceToast = Toast.makeText(getApplicationContext(), "Its working", Toast.LENGTH_SHORT);
pieceToast.show();

if(button.getId()== R.id.expBtnServer)
{
exportContactsToMyAccomut();
}
}


}


When I press the button, the control never comes to the onClick method.

Can you please help.
adolfchacko
Freshman
Freshman
 
Posts: 3
Joined: Sat Jul 02, 2011 9:59 am

Top

Re: Onclick event does not fire for a button

Postby Sowmya » Tue Jul 12, 2011 3:22 pm

Please comment the setContentView(R.layout.main);You have again set the View.

Code: Select all
this.expBtnServer.setOnClickListener(this);
//expBtnXML =(Button)this.findViewById(R.id.exprtBtnXML);
//expBtnXML.setOnClickListener(this);
et.setText(contactDetails.toString());

//setContentView(R.layout.main);
Toast pieceToast = Toast.makeText(getApplicationContext(), "On Create", Toast.LENGTH_LONG);
pieceToast.show();
Sowmya
Experienced Developer
Experienced Developer
 
Posts: 51
Joined: Fri Mar 06, 2009 7:49 am

Re: Onclick event does not fire for a button

Postby raj_J2ME » Thu Jul 14, 2011 10:47 am

Yeah, after removing the last setContentView(), plz try to clear and clean the project. There are lots of the examples on the web, if still it is not getting worked.
Thanks with Regards,
Raj - The Kernel
raj_J2ME
Master Developer
Master Developer
 
Posts: 311
Joined: Tue Feb 15, 2011 7:40 am
Location: The Capital of India,

Re: Onclick event does not fire for a button

Postby techkanika » Wed Aug 10, 2011 7:00 am

hi,

yeah.. after removing setContentView(R.layout.main) , your code will run perfectly..

kanika
Thanks
Kanika Rakheja
User avatar
techkanika
Experienced Developer
Experienced Developer
 
Posts: 83
Joined: Fri May 28, 2010 7:49 am

Top

Return to SDK/ADT/Emulator Problems

Who is online

Users browsing this forum: No registered users and 2 guests