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 6019241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:23:32+00:00 2026-05-23T03:23:32+00:00

I have extended the ImageView class in a class called DialButton2 (dont worry about

  • 0

I have extended the ImageView class in a class called DialButton2 (dont worry about the name of the class, its irrelevant). All the DialButton2 class does is show an arbitrary image located in the drawable folder.

package com.com.com;

import android.content.Context;
import android.widget.ImageView;
import android.util.AttributeSet;

public class DialButton2 extends ImageView{

    public DialButton2(Context context) {
        super(context);
        this.setImageResource(R.drawable.dialpad);
    }

    public DialButton2(Context context, AttributeSet attrs){
        super(context, attrs);
        this.setImageResource(R.drawable.dialpad);
    }

}

In the XML file for the main activity in my app I specify that a DialButton2 object should be displayed. I give it the id “button1”.

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="1"
    >
   <TableRow
    android:layout_weight="1">
         <DialButton
        android:id="@+id/button1"
        android:src="@drawable/dialpad"
        android:layout_weight="1"
        />

Dont worry about the rest of the XML-file, its irrelevant.

My problem is that when I try to instantiate a reference to the button in code, eclipse tells me I have to cast it to ImageView. Why is this?

package com.com.com;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

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

public class Android3 extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.maintable);

        /*
         * The instantiating below gives an error saying I have to cast to ImageView.
         */

        DialButton2 button1 = findViewById(R.id.button1);
    }
}
  • 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-05-23T03:23:32+00:00Added an answer on May 23, 2026 at 3:23 am

    You have to cast to DialButton2

        DialButton2 button1 = (DialButton2) findViewById(R.id.button1);
    

    findViewById() returns a View, you have to cast it before you can use it as a DialButton2.

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

Sidebar

Related Questions

I have an extended dialog class that I want to show for 3 seconds
I have an extended BaseAdapter in a ListActivity: private static class RequestAdapter extends BaseAdapter
I have extended my entities to implement specific interfaces for its type. I am
I have extended the ActiveRecord::Base class as follows: lib/activerecord_ext.rb: class ActiveRecord::Base named_scope( :recent, :conditions
I have extended Django's User Model using a custom user profile called UserExtension .
I have an extended ImageView that I'm reusing 7 times horizontally (within a LinearLayout)
Greetings all, As seen in the picture I have an extended QWidget object (which
I have extended a class like so: public class CormantRadDock : Telerik.Web.UI.RadDock { public
Greetings all, I have extended my own QAbstractListModel to change the background color of
I have a SharePoint site extended for forms authentication. The Active Directory site is

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.