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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:16:59+00:00 2026-06-04T09:16:59+00:00

I have defined a class XX like this: public class XX extends RelativeLayout {

  • 0

I have defined a class XX like this:

   public class XX extends RelativeLayout {
    protected static final boolean DEBUG = true;

    public XX(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    }

    public XX(Context context, AttributeSet attrs) {
    super(context, attrs);
    if (DEBUG)
        Log.i(this.getClass().getSimpleName(), " ->2"
            + Thread.currentThread().getStackTrace()[2].getMethodName());
    //getAttributes(context, attrs);

    }

    public XX(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    if (DEBUG)
        Log.i(this.getClass().getSimpleName(), " ->3"
            + Thread.currentThread().getStackTrace()[2].getMethodName());
    //getAttributes(context, attrs);

    }

}

and in my code I write:

RelativeLayout v = (RelativeLayout) this.findViewById(R.id.switch_TCMyTB);
XX x = (XX) v;  //<----- crashes here

but it crashes with the assignment. I assume the because XX extends View I can just assign the view (RelativeLayout) to an XX object.

But it crashes. What is wrong with the assignment?

EDIT:

I changed extends View to extends RelativeLayout. Also changed View v to RelativeLayout v.
But I still get a classCastException..???? Why?

While

RelativeLayout r = (RelativeLayout) v; 

certainly works fine.

  • 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-04T09:17:01+00:00Added an answer on June 4, 2026 at 9:17 am

    Since I’m not completely familiar with custom components, I just tried to make an example.
    I can’t answer your question as to why it wont work. If my example won’t run for you, you will need to provide a logcat.

    I create a custom class:

    public class TestLayout extends RelativeLayout {
    
        public TestLayout(Context context) {
            super(context);
    
        }
    
        public TestLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
    
        }
    
        public TestLayout(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    }
    

    This class is in the package com.test.cc

    In an XML layout I use

        <com.test.cc.TestLayout 
        android:layout_width="10dip"
        android:layout_height="10dip"
        android:id="@+id/testLayout"    
        />
    

    In this example layout.. TestLayout is a child of a LinearLayout.
    Add xmlns:android="http://schemas.android.com/apk/res/android" if its the highest level component in an xml layout.

    Then in the activity:

    //Called in onCreate mthod of an activity.    
    setContentView(R.layout.test); //make sure you call this first
    TestLayout l = (TestLayout)findViewById(R.id.testLayout);
    

    This runs fine for me.

    This was tested on a 2.2 and 3.2 device.

    So make sure you call setContentView(…) first and then create an object of the layout.
    Also make sure in the xml deffinition the package is right. Although if this was wrong you would get an class not foudn exception

    EDIT||

    I tried running this:

    RelativeLayout l = (RelativeLayout)findViewById(R.id.testLayout);
    TestLayout tl = (TestLayout)l;
    

    And it also runs fine without any exceptions. So I assume the problem lies elsewhere. Maybe package name errors or something.

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

Sidebar

Related Questions

I have a custom listView defined like this public class DDListView extends ListView implements
I have a Customer class defined like this: @Entity public class Customer { //...
I have a ViewModel defined like this: public class LocationTreeViewModel<TTree> : ObservableCollection<TTree>, INotifyPropertyChanged TTree
So I have defined a model like this: class Model extends Zend_Db_Table_Abstract { $_primary
I have a ViewFlipper defined in a separate class. It looks like this: public
I have a class defined like so: public class AddRecordsToolbar<D extends IDataSource<T>, T extends
I have a method that looks like this public static <T extends MyClass, X
I have a class that defines its own enum like this: public class Test
I have a class defined like this, with the appropriate getter and setter methods...
We have a class Car defined like this in a car.rb file class Car

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.