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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:20:03+00:00 2026-05-15T00:20:03+00:00

I’ve run into an early problem with developing for android. I’ve made my own

  • 0

I’ve run into an early problem with developing for android. I’ve made my own custom View (which works well). In the beginning i just added it to the layout programmatically, but i figured i could try putting it into the XML layout instead (for consistency).

So what i got is this:

main.xml:

[...]
<sailmeter.gui.CompassView
android:id="@+id/compassview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/widget55"
android:background="@color/white"
/>
[...]

CompassView.java:

public class CompassView extends View { 
protected void onDraw(Canvas canvas) { [...] }
public void setBearing(float bearing) { [...] }
}

SailMeter.java (activity class):

public class SailMeter extends Activity implements PropertyChangeListener {

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
compassview = (CompassView) findViewById(R.id.compassview1); //Line 51 in SailmMeter.java
[...]
}
}

(Theres obviously more, but you get the point)

Now, this is the stacktrace:

05-23 16:32:01.991: ERROR/AndroidRuntime(10742): Uncaught handler: thread main exiting due to uncaught exception  
05-23 16:32:02.051: ERROR/AndroidRuntime(10742): java.lang.RuntimeException: Unable to start activity ComponentInfo{sailmeter.gui/sailmeter.gui.SailMeter}:   java.lang.ClassCastException: android.view.View
05-23 16:32:02.051: ERROR/AndroidRuntime(10742):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
05-23 16:32:02.051: ERROR/AndroidRuntime(10742):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2621)
05-23 16:32:02.051: ERROR/AndroidRuntime(10742):     at android.app.ActivityThread.access$2200(ActivityThread.java:126)
05-23 16:32:02.051: ERROR/AndroidRuntime(10742):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1932)
05-23 16:32:02.051: ERROR/AndroidRuntime(10742):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-23 16:32:02.051: ERROR/AndroidRuntime(10742):     at android.os.Looper.loop(Looper.java:123)
05-23 16:32:02.051: ERROR/AndroidRuntime(10742):     at android.app.ActivityThread.main(ActivityThread.java:4595)
05-23 16:32:02.051: ERROR/AndroidRuntime(10742):     at java.lang.reflect.Method.invokeNative(Native Method)
05-23 16:32:02.051: ERROR/AndroidRuntime(10742):     at java.lang.reflect.Method.invoke(Method.java:521)
05-23 16:32:02.051: ERROR/AndroidRuntime(10742):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-23 16:32:02.051: ERROR/AndroidRuntime(10742):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-23 16:32:02.051: ERROR/AndroidRuntime(10742):     at dalvik.system.NativeStart.main(Native Method)
05-23 16:32:02.051: ERROR/AndroidRuntime(10742): Caused by: java.lang.ClassCastException: android.view.View
05-23 16:32:02.051: ERROR/AndroidRuntime(10742):     at sailmeter.gui.SailMeter.onCreate(SailMeter.java:51)
05-23 16:32:02.051: ERROR/AndroidRuntime(10742):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-23 16:32:02.051: ERROR/AndroidRuntime(10742):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2544)
05-23 16:32:02.051: ERROR/AndroidRuntime(10742):     ... 11 more

Why cant i cast my custom view? I need it to be that type since it has a few extra methods in it that i want to access. Should i restructure it and have another class handle the logic, and then just having the view being a view? I’d really like this to work though.

Thanks for any help.

  • 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-15T00:20:04+00:00Added an answer on May 15, 2026 at 12:20 am

    I’ve done this before and found it necessary to use a LayoutInflater. Perhaps you could try something like this:

    public CompassView( Context context, AttributeSet attributeSet ) 
    {
        super( context, attributeSet );
    
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate( R.layout.name_of_your_compass_view_layout_file, this );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 428k
  • Answers 428k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer RewriteRule /?sitemap.xml %{DOCUMENT_ROOT}/sitemaps/%{HTTP_HOST}.xml [L] May 15, 2026 at 1:24 pm
  • Editorial Team
    Editorial Team added an answer Have you seen the DateFormatter class? Example: import mx.formatters.DateFormatter; private… May 15, 2026 at 1:24 pm
  • Editorial Team
    Editorial Team added an answer Well, already figured it out, sorry anyone that wasted their… May 15, 2026 at 1:24 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.