我有一个问题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)
最佳答案
是的,拉里是对的-片段不会立即添加。如果您将片段附加到活动之后要在片段中完成工作,那么一个不错的选择是重写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回调您的活动,因此该片段告诉活动已准备好对其进行初始化。
那有意义吗?如果您告诉我更多细节,我会更好地建议您。