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

  • Home
  • SEARCH
  • 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 8170129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:04:24+00:00 2026-06-06T21:04:24+00:00

I’m trying to implement a Service and I have a classCastException in my Service.

  • 0

I’m trying to implement a Service and I have a classCastException in my Service.
Since Location isn’t Parcelable I wrap it in a ParcelableArrayList.

public class GPSService extends Service {
    public void setLocation(Location location) {
        ArrayList<Location> locationArrayList = new ArrayList<Location>(1);
        locationArrayList.add(location);
        Bundle b = new Bundle();
        b.putParcelableArrayList("location", locationArrayList);
        // try with an arrayList instead of just getExtra doesn't change anything :(
        ArrayList<GPSResultReceiver> resultReceiverArrayList = intent.getParcelableArrayListExtra("receiver");
        // this is a resultReceiver and not a GPSResultReceiver
        // where did the upcast happen?
        GPSResultReceiver receiver = resultReceiverArrayList.get(0);
        Log.d("ResultClass", receiver.getClass().toString());
        receiver.onReceiveResult(1111, b);
    }

My Activity looks as following:

public class MyActivity extends Activity implements GPSResultReceiver.Receiver {
    void startService() {
        resultReceiver = new GPSResultReceiver(new Handler());
        resultReceiver.setReceiver(this);
        final Intent intent = new Intent(Intent.ACTION_SEND, null, this, GPSService.class);
        // intent.putExtra("receiver", resultReceiver);
        ArrayList<GPSResultReceiver> resultReceiverArrayList = new ArrayList<GPSResultReceiver>(1);
        resultReceiverArrayList.add(resultReceiver);
        intent.putParcelableArrayListExtra("receiver", resultReceiverArrayList);
        serviceRunning = true;
        startService(intent);
    }

My GPSResultReceiver:

public class GPSResultReceiver extends ResultReceiver {
    private Receiver resultReceiver;
    public GPSResultReceiver(Handler handler) {
        super(handler);
    }
    public void setReceiver(Receiver receiver) {
        resultReceiver = receiver;
    }
    public interface Receiver {
        public void onReceiveResult(int resultCode, Bundle resultData);
    }
    @Override
    public void onReceiveResult(int resultCode, Bundle resultData) {
        if (resultReceiver != null) {
            resultReceiver.onReceiveResult(resultCode, resultData);
        }
    }
}

So why does this cast occur here and how can I work around it?

Thanks in advance

  • 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-06T21:04:24+00:00Added an answer on June 6, 2026 at 9:04 pm

    Seems it works as expected, because CREATOR of ResultReceiver creates ResultReceiver, but not it’s child classes.
    The point here is following: actually, You don’t need GPSResultReceiver in the service. Just ResultReceiver is quite enough to send results back, but You should call public void send (int resultCode, Bundle resultData), but not onReceiveResult.
    So, it would look like

    Service:

    public class GPSService extends Service {
    
        Intent intent = null;
        ResultReceiver receiver = null;
    
        @Override
        public IBinder onBind(final Intent arg0) {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public void onStart(final Intent inIntent, final int startId) {
    
            super.onStart(inIntent, startId);
    
            this.intent = inIntent;
            setLocation(null);
        }
    
        void setLocation(final Location location) {
            final ArrayList<Location> locationArrayList = new ArrayList<Location>(1);
            locationArrayList.add(location);
            final Bundle b = new Bundle();
            b.putParcelableArrayList("location", locationArrayList);
    
            try {
                this.receiver = this.intent.getParcelableExtra("com.example.testservice.TestResultReceiver");
            } catch (final ClassCastException e) {
                Log.e("GPSService", "Failed to cast to  GPSResultReceiver", e);
            }
    
            try {
                this.receiver = GPSResultReceiver.class.cast(this.intent.getParcelableExtra("com.example.testservice.TestResultReceiver"));
            } catch (final ClassCastException e) {
                Log.e("GPSService", "Failed to cast to  GPSResultReceiver 2", e);
            }
    
            Log.d("ResultClass", this.receiver.getClass().toString());
            this.receiver.send(1111, b);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:

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.