gpt4 book ai didi

Android findFragmentByTag 返回错误的 fragment

转载 作者:行者123 更新时间:2023-11-30 03:16:04 26 4
gpt4 key购买 nike

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

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

首先:

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)

最佳答案

问题出在这一行:

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

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

结果正确代码是:

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

关于Android findFragmentByTag 返回错误的 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20122946/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com