有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

安卓致命异常:主java。lang.RuntimeException:被问及未知片段

我有一个查看寻呼机和3个片段。 我的应用程序对其中2个运行良好,但当我添加第三个片段时,它会返回以下错误:

12-17 14:11:30.969: ERROR/AndroidRuntime(7350): FATAL EXCEPTION: main
        java.lang.RuntimeException: got asked about an unknown fragment

我的部分代码如下所示:

if (position == 0) {
      if (mContactList == null)
          mContactList = new ContactListFragment();
                return mContactList;
} else if (position == 1) {
      if (mContact == null)
          mContact= new ContactFragment(); // my third fragment  
          return mContact ;
} else {
      int positionMod = position - 2;
      mCursor.moveToPosition(positionMod);
      long contactChatId = mCursor.getLong(ChatView.CONTACT_ID_COLUMN);
      String contactName = mCursor.getString(ChatView.USERNAME_COLUMN);
      long providerId = mCursor.getLong(ChatView.PROVIDER_COLUMN);
      return ChatViewFragment.newInstance(contactChatId, contactName, providerId);
}

共 (1) 个答案

  1. # 1 楼答案

    我认为您正在尝试引用当前片段中的其他片段。 尝试将setOffscreenPageLimit设置为2。如文件所述:

    Set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state. Pages beyond this limit will be recreated from the adapter when needed.

    使用此代码:

    ViewPager mViewPager = (ViewPager) findViewById(R.id.historypager);
    mViewPager.setOffscreenPageLimit(2);