Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9007307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:39:10+00:00 2026-06-16T01:39:10+00:00

I am new to the onClickListener. I am trying to learn it using example

  • 0

I am new to the onClickListener. I am trying to learn it using example code from stackoverflow and android (http://developer.android.com/reference/android/widget/Button.html), but I cannot get my code to compile. I even copy and pasted the code directly from android, and it still would not compile. So my question is:

Can you recommend a beginners tutorial to learn the onClickListener in depth? Thanks.

Even when I used the identical code from android, I would still get the same errors. But in case its relevant to my question above, here is my code and error:

package com.evorlor.testcode;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

public class Button extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_button);

        Button btnTest = (Button) findViewById(R.id.btnTest);
        btnTest.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

            }
        });

    }

}

Cannot cast from View to Button
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:39:12+00:00Added an answer on June 16, 2026 at 1:39 am

    That tutorial snippet refers to the android Button class. You wrote your own class and named it “Button” – hence, the cast won’t work. Your button extends “Activity” which isn’t a subclass of view, so you can’t cast a View to your Button class.

    Rename your class, and import android.widget.button

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

trying to send Arabic text from android to PHP file using the code below
I was trying to learn by following the lab here https://sites.google.com/site/androidcoursearchive/labs/lab-1 Here's the code
Code: button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // fileReader(); new xyz().execute(); }//
New programmer here, I am trying to understand and break down this code below
New developer here,Im using the Custom Image Picker by ray wenderlich. But what I
I'm new at android developing. I'm trying to make a simple drumset app, when
I am trying to compile Stephen Wylie's example code for Google Drive integration: package
I'm trying to execute this JSON call as follows: button.setOnClickListener(new OnClickListener() { @Override public
I am trying to get away from using 5 buttons when I would like
I am new to android programming and trying to use webservice in this sample

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.