我有一个片段开始另一个片段。我需要从第二个访问第一个片段的方法。但是我遇到了一个奇怪的错误。
有没有人有解决这个问题的想法?
第一的:
public class ArticleFragment extends Fragment implements AdapterView.OnItemClickListener, View.OnTouchListener {
public static final String TAG = ArticleFragment.class.getClass().getSimpleName();
...
ArticleEditFragment mFrag = new ArticleEditFragment();
FragmentTransaction t = getActivity().getSupportFragmentManager().beginTransaction();
t.replace(R.id.fragment_container, mFrag, ArticleEditFragment.TAG);
t.addToBackStack(ArticleEditFragment.TAG);
t.commit();
第二:
public class ArticleEditFragment extends Fragment implements View.OnTouchListener, View.OnClickListener {
public static final String TAG = ArticleEditFragment.class.getClass().getSimpleName();
...
ArticleFragment articleFragment = ((ArticleFragment) getActivity().getSupportFragmentManager()
.findFragmentByTag(ArticleFragment.TAG));
错误:
java.lang.ClassCastException: com.ThreeBoots.fragments.ArticleEditFragment cannot be cast to com.ThreeBoots.fragments.ArticleFragment
at com.ThreeBoots.fragments.ArticleEditFragment.saveClicked(ArticleEditFragment.java:101)
at com.ThreeBoots.fragments.ArticleEditFragment.onOptionsItemSelected(ArticleEditFragment.java:81)
at android.support.v4.app.Fragment.performOptionsItemSelected(Fragment.java:1608)
at android.support.v4.app.FragmentManagerImpl.dispatchOptionsItemSelected(FragmentManager.java:2001)
at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:378)
at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:958)
at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:514)
at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:99)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)