1

我有一个片段开始另一个片段。我需要从第二个访问第一个片段的方法。但是我遇到了一个奇怪的错误。

有没有人有解决这个问题的想法?

第一的:

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)
4

2 回答 2

1

问题出在这一行:

public static final String TAG = ArticleFragment.class.getClass().getSimpleName();

它不会像ArticleFragment那样返回类名。我在控制台中只看到了Class.getClass() ,因为我添加了这个方法。当我删除它时,问题就解决了。

结果正确的代码是:

 public static final String TAG = ArticleFragment.class.getSimpleName();
于 2013-11-21T17:19:49.280 回答
0

你用

t.replace(R.id.fragment_container, mFrag, ArticleEditFragment.TAG);

但试图用另一个标签检索它

ArticleFragment articleFragment = ((ArticleFragment) getActivity().getSupportFragmentManager()
        .findFragmentByTag(ArticleFragment.TAG));

您应该使用同一型号的 TAG。

于 2013-11-21T14:09:17.210 回答