findFragmentByTag不起作用

时间:2014-11-30 19:14:33

标签: java android

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

我在支持v4时尝试过相同的东西,但这总是让我返回null

MainActivity:`

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();
    }
}

ContentFragment:`

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)

1 个答案:

答案 0 :(得分:1)

是的,拉里是对的 - 片段不会立即添加。一个好的选择,如果你在片段附加到活动后要做的工作,就是覆盖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检索您的片段:

getFragmentManager().findFragmentByTag("mine");

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

这有意义吗?如果你告诉我更多细节,我可以给你更好的建议。