Android中的嵌套片段会产生IllegalStateException:无活动

时间:2015-10-17 17:14:22

标签: java android android-fragments

我试图在Android SDK 17中嵌套片段,但我不断收到IllegalStateException,声明没有Activity。我已经看到其他帖子可以追溯到2011年,这是一个错误。我无法相信四年后的许多Android版本以后这个问题仍然存在。有人可以告诉我如何在没有异常的情况下嵌套片段吗?

这是我正在使用的实现。

import android.app.Fragment;
import android.app.FragmentManager;

public class HomeActivity extends AppCompatActivity 
    implements AdapterView.OnItemClickListener,
    SinglePaneContainerFragment.OnFragmentInteractionListener {

private Fragment containerFragment;
...

containerFragment = new SinglePaneContainerFragment();

getFragmentManager().beginTransaction().replace(
     R.id.content_frame, containerFragment, "single_pain").commit();

FragmentManager childFragmentManager = containerFragment.getChildFragmentManager();

childFragmentManager.beginTransaction().replace(
      R.id.fragment_placeholder1, new NestedFragment(), "featured").commit();
  

java.lang.IllegalStateException:没有活动               在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1069)               在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1064)               在android.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1884)               在android.app.Fragment.performActivityCreated(Fragment.java:2128)               在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)               在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)               在android.app.BackStackRecord.run(BackStackRecord.java:834)               在android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)               在android.app.FragmentManagerImpl $ 1.run(FragmentManager.java:452)               在android.os.Handler.handleCallback(Handler.java:739)               在android.os.Handler.dispatchMessage(Handler.java:95)               在android.os.Looper.loop(Looper.java:145)               在android.app.ActivityThread.main(ActivityThread.java:6837)               at java.lang.reflect.Method.invoke(Native Method)               在java.lang.reflect.Method.invoke(Method.java:372)               在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1404)               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

0 个答案:

没有答案