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.
From docs:
In your code
line throws
ClassCastException
because your activity don't implementSetupHotspotDialogListener
interface. (Fragment
is directly tied to the activity that contains it, as well asDialogFragment
becauseDialogFragment
extendsFragment
).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
fromFragment
I suggest to organize it via callbacks to activity:SmartMode
Fragment
class (like you do into dialogFragment) with one method likecreateDialogRequest()
.Fragment
toActivity
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 receivingfragment dialog
callback events into fragment.Activity:
Fragment:
FragmentDialog:
I used support 4 library fragments
And layouts:
activity_my_fragment.xml
:fragment.xml
:fragment_dialog.xml
:The idea is to send reference to interface which will catch callback events.