Android findFragmentByTag返回错误的片段

时间:2013-11-21 13:59:55

标签: android android-fragments android-activity android-support-library fragmentmanager

我有一个片段可以启动另一个片段。我需要从第二个访问第一个片段的方法。但我得到了一个奇怪的错误。

有人有想法解决这个问题吗?

首先:

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)

2 个答案:

答案 0 :(得分:1)

问题在于这一行:

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

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

结果正确的代码是:

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

答案 1 :(得分:0)

使用

标记片段
t.replace(R.id.fragment_container, mFrag, ArticleEditFragment.TAG);

但是尝试用另一个标签检索它

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

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