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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:00:47+00:00 2026-06-18T09:00:47+00:00

My DialogFragment throws ClassCastException if called from Fragment, while it is working normally if

  • 0

My DialogFragment throws ClassCastException if called from Fragment, while it is working normally if called from an Activity. I have already looked at few other questions with similar problem and basically those are related to imports, but I haven’t been able to solve it in my implementation. Here is my implementation for DialogFragment.

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;

public class HotspotScanDialog extends DialogFragment {

    SetupHotspotDialogListener mListener;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        ...

        .setAdapter(hotspotAdapter, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mListener.onHotspotSelectedListener(hotspotAdapter.getItem(
                        which).toString());
            }
        })...
    }

    public interface SetupHotspotDialogListener {
        public void onHotspotSelectedListener(String selection);

    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        try {
            mListener = (SetupHotspotDialogListener) activity;
        } catch (ClassCastException ignore) {
            // Just to make sure if anyone will be pointing at my throwing
            // ClassCastException myself I have tried without this code as well.
            throw new ClassCastException(activity.toString()
                    + " must implement NoticeDialogListener");
        }
    }
}

Here is my Fragment that is using the above DialogFragment:

import android.app.AlertDialog;
import android.app.DialogFragment;
import android.support.v4.app.Fragment;
import com.xxx.yyy.ui.compontent.dialog.HotspotScanDialog;
import com.xxx.yyy.ui.compontent.dialog.HotspotScanDialog.SetupHotspotDialogListener;

public class SmartMode extends Fragment implements SetupHotspotDialogListener {

    private void showWifiSelectionDialog() {
        DialogFragment setupWifiSelectionDialog = new HotspotScanDialog();

        /*
         * using getFragmentManager() only says "The method
         * show(FragmentManager, String) in the type DialogFragment is not
         * applicable for the arguments (FragmentManager, String)"
         */

        setupWifiSelectionDialog.show(getActivity().getFragmentManager(),
                Keys.TAG.toString());
    }

    @Override
    public void onHotspotSelectedListener(String selection) {
        // Log.d(TAG,selection);
    }
}

This is the error log:

02-01 13:11:32.750: E/AndroidRuntime(15061): FATAL EXCEPTION: main
02-01 13:11:32.750: E/AndroidRuntime(15061): java.lang.ClassCastException: com.milanix.tuki.UiMainActivity@41d75350 must implement NoticeDialogListener
02-01 13:11:32.750: E/AndroidRuntime(15061): at com.xxx.yyy.ui.compontent.dialog.HotspotScanDialog.onAttach(HotspotScanDialog.java:122)
02-01 13:11:32.750: E/AndroidRuntime(15061): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:787)
02-01 13:11:32.750: E/AndroidRuntime(15061): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
02-01 13:11:32.750: E/AndroidRuntime(15061): at android.app.BackStackRecord.run(BackStackRecord.java:635)
02-01 13:11:32.750: E/AndroidRuntime(15061): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
02-01 13:11:32.750: E/AndroidRuntime(15061): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
02-01 13:11:32.750: E/AndroidRuntime(15061): at android.os.Handler.handleCallback(Handler.java:615)
02-01 13:11:32.750: E/AndroidRuntime(15061): at android.os.Handler.dispatchMessage(Handler.java:92)
02-01 13:11:32.750: E/AndroidRuntime(15061): at android.os.Looper.loop(Looper.java:137)
02-01 13:11:32.750: E/AndroidRuntime(15061): at android.app.ActivityThread.main(ActivityThread.java:4898)
02-01 13:11:32.750: E/AndroidRuntime(15061): at java.lang.reflect.Method.invokeNative(Native Method)
02-01 13:11:32.750: E/AndroidRuntime(15061): at java.lang.reflect.Method.invoke(Method.java:511)
02-01 13:11:32.750: E/AndroidRuntime(15061): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
02-01 13:11:32.750: E/AndroidRuntime(15061): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
02-01 13:11:32.750: E/AndroidRuntime(15061): at dalvik.system.NativeStart.main(Native Method)

I am wondering if anyone can give a hint about this issue.

  • 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-18T09:00:48+00:00Added an answer on June 18, 2026 at 9:00 am

    From docs:

     onAttach(Activity) called once the fragment is associated with its activity.
    

    In your code

     mListener = (SetupHotspotDialogListener) activity;
    

    line throws ClassCastException because your activity don’t implement SetupHotspotDialogListener interface. (Fragment is directly tied to the activity that contains it, as well as DialogFragment because DialogFragment extends Fragment).

    Again from docs

    In some cases, you might need a fragment to share events with the activity. A good way to do that is to define a callback interface inside the fragment and require that the host activity implement it. When the activity receives a callback through the interface, it can share the information with other fragments in the layout as necessary.

    So if you want to create FragmentDialog from Fragment I suggest to organize it via callbacks to activity:

    1. create callback interface into your SmartMode Fragment class (like you do into dialogFragment) with one method like createDialogRequest().
    2. let your activity implement that interface
    3. then, when you need to create dialog, send callback from Fragment to Activity
    4. place “show dialog logics” into Activity

    It’s look like fragment ask activity to create dialog, activity shows dialog.

    EDITED:
    I think i had found better implementation of what you need. I’ve write a simple example of creating fragment dialog from fragment with receiving fragment dialog callback events into fragment.

    Activity:

    public class MyFragmentActivity extends FragmentActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_my_fragment);
    
            // first start of activity
            if (savedInstanceState == null) {
                // put fragment to activity layout 
                MyFragment fragment = new MyFragment();
                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                ft.replace(R.id.fragmentContainer, fragment, "fragment");
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                ft.commit();
            }
        }
    
    }
    

    Fragment:

    public class MyFragment extends Fragment implements MyDialogInterface {
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            super.onCreateView(inflater, container, savedInstanceState);
    
            View fragmentView = inflater.inflate(R.layout.fragment, null);
    
            // button which shows dialog
            Button showDialogButton = (Button) fragmentView.findViewById(R.id.showDialog);
            showDialogButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // create fragment dialog.
                    FragmentDialog dialog = FragmentDialog.getInstance(MyFragment.this);
                    dialog.show(getActivity().getSupportFragmentManager(), "dialog");
                }
            });
    
            return fragmentView;
        }
    
        @Override
        public void onClickEvent() {
            // receive click events from dialog fragment
            Log.e(getClass().getSimpleName(), "onClickEvent");
        }
    
    }
    

    FragmentDialog:

    public class FragmentDialog extends DialogFragment {
    
        public interface MyDialogInterface extends Serializable {
            public void onClickEvent();
        }
    
        private MyDialogInterface callbackListener;
    
        /**
         * dialogInterface - instance of MyDialogInterface which will handle
         * callback events
         */
        public static FragmentDialog getInstance(MyDialogInterface dialogInterface) {
            FragmentDialog fragmentDialog = new FragmentDialog();
    
            // set fragment arguments
            Bundle args = new Bundle();
            args.putSerializable("dialogInterface", dialogInterface);
            fragmentDialog.setArguments(args);
    
            return fragmentDialog;
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setStyle(STYLE_NO_TITLE, 0);
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    
            View pushDialogView = getActivity().getLayoutInflater().inflate(R.layout.fragment_dialog, null);
    
            // get reference to MyDialogInterface instance from arguments
            callbackListener = (MyDialogInterface) getArguments().getSerializable("dialogInterface");
    
            Button cancelButton = (Button) pushDialogView.findViewById(R.id.button);
            cancelButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // send click event
                    callbackListener.onClickEvent();
                }
            });
    
            return pushDialogView;
        }
    
    }
    

    I used support 4 library fragments

    android.support.v4.app.Fragment
    android.support.v4.app.DialogFragment
    android.support.v4.app.FragmentActivity
    

    And layouts:

    activity_my_fragment.xml :

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+id/fragmentContainer"
       android:layout_width="match_parent"
       android:layout_height="match_parent" />
    

    fragment.xml :

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:background="#a00"
       android:orientation="vertical" >
    
       <Button
         android:id="@+id/showDialog"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="show doalog" />
    </LinearLayout>
    

    fragment_dialog.xml :

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:background="#fe3"
       android:orientation="vertical" >
    
       <Button
          android:id="@+id/button"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="click me" />
     </LinearLayout>
    

    The idea is to send reference to interface which will catch callback events.

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

Sidebar

Related Questions

I have fragment that on a component click pop-ups DialogFragment. This dialog fragment holds
I have a DialogFragment which creates a DatePickerDialog. I'm using a static method called
I'm trying to display a dialogfragment from within a viewpager fragment. For some reason.
I am using Actionbarsherlock and have a ListFragment and a DialogFragment within my Fragment
Question: How does one create a callback from a DialogFragment to another Fragment. In
I have Fragment that extends DialogFragment and I have a custom layout for it
I have created an DialogFragment which is building and returning AlertDialog from onCreateDialog method.
EDIT: I have a DialogFragment where the reference inside the parent Activity is NULL
I have a DialogFragment from where i am adding the item to the database
I have DialogFragment and a Loader . Dialog prompts user to logout from Application.

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.