Crash on button create

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Crash on button create

Postby lostgame » Mon Nov 16, 2009 9:34 pm

No matter what I do, anytime I create a new android project (I'm using the 1.5 SDK) and create just one button, imagebutton, or the like, and then try to reference it in code, I get a crash. I've worked the last 4 hours on trying to just create one button. Coming from iPhone development, where I could create an entire simple application in 4 hours, this is ridiculous. >.<

I've included all the needed imports, android.widget.button, etc.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  3. setContentView(R.layout.main);
  5.         Button loginButton;
  7.         loginButton = (Button) this.findViewById(;
  9.         loginButton.setOnClickListener((OnClickListener) this);
Parsed in 0.031 seconds, using GeSHi

Results no matter what in:

11-16 15:28:23.217: ERROR/AndroidRuntime(827): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.avidlifemedia.hotornot/com.avidlifemedia.hotornot.mainscreen}: java.lang.ClassCastException: com.avidlifemedia.hotornot.mainscreen

Followed by about 20 more errors, many of which LogCat won't even let me see, simply stating "...11 more."

What on earth? 11 more? How do I see those 11 more! They could be the solution to this 4-hour problem!

Never in my ten years of professional programming experience have I been so frustrated.
Junior Developer
Junior Developer
Posts: 18
Joined: Thu Apr 02, 2009 8:59 pm


Postby qlimax » Tue Nov 17, 2009 1:23 am

i think class cast error is because your class is not implementing OnClickListener interface...

when you implement this interface, this casting is not required

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. (OnClickListener) this
Parsed in 0.031 seconds, using GeSHi

just set this as clicklistener, because
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. this
Parsed in 0.034 seconds, using GeSHi
object is of the type onclicklistener (you must implements OnClickListener at the class level).


you can do the click stuff ,also without implemeting the clicklistener interface ->>declaring it "on the fly"


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. loginButton.setOnClickListener(new OnClickListener() {
  3.                         public void onClick(View v) {
  4.                                 // do click stuff
  6.                         }
  7.                 });
Parsed in 0.035 seconds, using GeSHi

and if this does not work, can you post the full logcat error?

¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
Master Developer
Master Developer
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Postby Kur1310 » Tue Nov 17, 2009 5:25 am

Hi lostgame,,
U might be getting the classcast exception because u might have taken an imagebutton in ur layout file and in java code u r trying to cast it in a button....
That might be ur problem.. Just check it out.... :shock:
Junior Developer
Junior Developer
Posts: 20
Joined: Wed Oct 28, 2009 10:50 am


Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 4 guests