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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:49:05+00:00 2026-06-17T19:49:05+00:00

I have a callback class gaGPS which requires I think is a context which

  • 0

I have a callback class gaGPS which requires I think is a context which works fine in a normal activity but when I use in a Fragment it fails

here is my interface class

Public class gaGPS extends Service implements LocationListener {

private final Context mContext;

// Declaring a Location Manager
protected LocationManager locationManager;

private gaGPSinterface gpsu;

public interface gaGPSinterface {
    public void locationChanged(Location newloc);
}


public gaGPS(Context c, gaGPSinterface gpsiface) {
    mCallback = gpsiface;
    //mContext = (Context) gpsiface;
    mContext = c;


    locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
}

snip

I’m launching the Fragment using this FragmentActivity class

public class gaDetailsActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gpsfragment);

}

here is the gpsfragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".gpsfragment" >
<fragment
    android:id="@+id/gpsTestFragLayout"
    android:name="id.wilsononline.gcadroid.gpsfragment"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1" />

the following is the Fragment I’m using

public class gpsfragment extends Fragment implements gaGPS.gaGPSinterface {

private gaGPS gps;

@Override
public void locationChanged(Location newloc) {
    // TODO Auto-generated method stub

}

@Override
public void onAttach(Activity activity) {
    // TODO Auto-generated method stub
    super.onAttach(activity);
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View rootView = inflater.inflate(R.layout.gpsdetails, container, false);
    ((TextView) rootView.findViewById(R.id.gpsText1)).setText("testing fragment");

    Context c = getActivity().getApplicationContext();

    gps = new gaGPS(c, (gaGPSinterface) getActivity());

    return rootView;
}

}

errors I’m getting are :
java.lang.ClassCastException: com.test.app.gpsfragment cannot be cast to android.content.Context

more fuller output:

E/AndroidRuntime( 3118): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
E/AndroidRuntime( 3118):        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
E/AndroidRuntime( 3118):        at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
E/AndroidRuntime( 3118):        at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
E/AndroidRuntime( 3118):        at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
E/AndroidRuntime( 3118):        at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
E/AndroidRuntime( 3118):        at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
E/AndroidRuntime( 3118):        at android.com.test.app.Activity.setContentView(Activity.java:1867)
E/AndroidRuntime( 3118):        at id.wilsononline.gcadroid.gaDetailsActivity.onCreate(gaDetailsActivity.java:12)
E/AndroidRuntime( 3118):        at android.com.test.app.Activity.performCreate(Activity.java:5008)
E/AndroidRuntime( 3118):        at android.com.test.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
E/AndroidRuntime( 3118):        at android.com.test.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
E/AndroidRuntime( 3118):        ... 11 more
E/AndroidRuntime( 3118): Caused by: java.lang.ClassCastException: com.test.com.test.app.gaDetailsActivity cannot be cast to id.wilsononli
ne.gcadroid.gaGPS$gaGPSinterface
E/AndroidRuntime( 3118):        at com.test.com.test.app.gpsfragment.onCreateView(gpsfragment.java:49)
E/AndroidRuntime( 3118):        at android.support.v4.com.test.app.Fragment.performCreateView(Fragment.java:1460)
E/AndroidRuntime( 3118):        at android.support.v4.com.test.app.FragmentManagerImpl.moveToState(FragmentManager.java:884)
E/AndroidRuntime( 3118):        at android.support.v4.com.test.app.FragmentManagerImpl.moveToState(FragmentManager.java:1066)
E/AndroidRuntime( 3118):        at android.support.v4.com.test.app.FragmentManagerImpl.addFragment(FragmentManager.java:1168)
E/AndroidRuntime( 3118):        at android.support.v4.com.test.app.FragmentActivity.onCreateView(FragmentActivity.java:280)
E/AndroidRuntime( 3118):        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
E/AndroidRuntime( 3118):        ... 21 more
  • 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-17T19:49:08+00:00Added an answer on June 17, 2026 at 7:49 pm
    public gaGPS(gaGPSinterface gpsu) {
        this.mContext = (Context) gpsu;
        this.gpsu = gpsu;
        // ...
    }
    

    Here, you’re trying to cast gpsu to a Context. It’s is not one; it’s a Fragment. You should pass the Context and gaGPSinterface separately:

    public gaGPS(Context c, gaGPSinterface gpsu) {
        this.mContext = c;
        this.gpsu = gpsu;
        // ...
    }
    

    Then, call it using .getActivity() (which returns an Activity, a subclass of Context) from the Fragment:

    gps = new gaGPS(getActivity(), this);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Android class which has a callback from an asynchronous HTTP process,
I have a member function in a class that has a callback, but the
Here I create a class in JAVA in which I have function (callback) which
I have this activity: public final class CaptureActivity extends Activity implements SurfaceHolder.Callback { }
I'm writing some callback implementation in C++. I have an abstract callback class, let's
I have a working wrapper class for Dallmeier camera devices, It contains a callback
I have a fairly generic class (Record) that I have added a callback handler
We have a situation where we use the WebClient class to download some data
Creating a class which holds some threads, performing tasks and finally calling a callback-method
I have a callback function for CURLOPT_PROGRESSFUNCTION in codeigniter like below: class download_model extends

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.