gpt4 book ai didi

java - findFragmentByTag 不起作用

转载 作者:行者123 更新时间:2023-12-01 12:11:58 25 4
gpt4 key购买 nike

我有一个问题 findFragmentByTag 总是返回 null;所以我想解决这个问题。

我在支持 v4 上尝试了相同的操作,但这总是返回 null

主要 Activity :`

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

FragmentTransaction t = getFragmentManager().beginTransaction();
ContentFragment temp = new ContentFragment();
t.add(R.id.content, temp, "mine");

// it does the same
//t.add(R.id.content, new ContentFragment(), "mine");
//t.commitAllowingStateLoss(); // from supprt v4
t.commit();

//
ContentFragment fragment = (ContentFragment) getFragmentManager().findFragmentByTag("mine");
if (fragment == null) Log.e("ALARM", "fragment == null");
else fragment.callMe();
}
}

内容 fragment :`

public class ContentFragment extends ListFragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
inflater.getContext(),
android.R.layout.simple_list_item_1,
Environment.getRootDirectory().list());

setListAdapter(adapter);

return super.onCreateView(inflater, container, savedInstanceState);
}

public void callMe(){
Toast.makeText(this.getActivity().getBaseContext(), this.getTag(), Toast.LENGTH_LONG).show();
}
}`

main.xml

<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"/>

跟踪

11-30 22:37:41.824  13491-13491/? E/Trace﹕ error opening trace file: No such file or directory (2)
11-30 22:37:55.271 13526-13526/? E/Trace﹕ error opening trace file: No such file or directory (2)
11-30 22:37:56.492 13569-13569/by.tq.filemanager.app E/Trace﹕ error opening trace file: No such file or directory (2)
11-30 22:37:58.361 13569-13569/by.tq.filemanager.app E/ERR﹕ java.lang.NullPointerException
at by.tq.filemanager.app.MainActivity.onCreate(MainActivity.java:30)
at android.app.Activity.performCreate(Activity.java:5122)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1146)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2315)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.access$600(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5370)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)

最佳答案

是的,拉里是对的—— fragment 不会立即添加。如果您在将 fragment 附加到 Activity 后还有工作要做,一个不错的选择是重写 onAttach:

public class ContentFragment extends ListFragment {
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
Toast.makeText(this.getActivity().getBaseContext(), this.getTag(), Toast.LENGTH_LONG).show();
}
}

编辑

您稍后可以使用 findFragmentByTag 从 Activity 中检索 fragment :

getFragmentManager().findFragmentByTag("mine");

主要问题是您无法在将 fragment 添加到 Activity 后立即获取 fragment 。何时访问 fragment 的问题实际上取决于您需要在生命周期中的何时执行该工作。如果您正在响应用户输入,那么您当然可以在那时调用 findFragmentByTag 。如果是fragment的初始化,可以使用onAttach或onCreateView。您甚至可以从 onAttach 回调您的 Activity,因此 fragment 告诉 Activity 它已准备好初始化。

这有道理吗?如果您告诉我更多细节,我可以为您提供更好的建议。

关于java - findFragmentByTag 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27217136/

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