DialogFragment中的ViewPager + FragmentPagerAdapter获取“IllegalArgumentException:No view found …”

我试图显示一个FragmentDialog(创build并显示为一个对话框不添加为视图层次结构中的内容),其中有一个ViewPager的内容是由FragmentPagerAdapter(提供的碎片组成的图像)​​给出的。

当从FragmentActivity中显示ViewPager + FragmentPagerAdapter时,代码工作正常,但从FragmentDialog中获取以下exception:

“IllegalArgumentException:为片段SimpleFragment没有发现ID为0x7f040077 …”

这是我的代码:

SherlockFragmentActivity带有一个button来创build和显示对话框。

public class BorrameActivity extends SherlockFragmentActivity{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.one_act); Button button = (Button) findViewById(R.id.button); button.setOnClickListener(new OnClickListener(){ @Override public void onClick(View arg0) { showTheDialog(); }}); } private void showTheDialog(){ AchGalleryDialog newFragment = AchGalleryDialog.newInstance(achs); newFragment.show(getSupportFragmentManager(), "dialog"); } 

FragmentDialog:

 public class AchGalleryDialog extends DialogFragment{ public AchGalleryDialog(){ } public static AchGalleryDialog newInstance(){ AchGalleryDialog f = new AchGalleryDialog(); return f; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.dialog_user_result, container); getDialog().setTitle("Hola tronco"); //content to show in the fragments int[] images = new int[]{R.drawable.d1, R.drawable.d2, R.drawable.d3}; ViewPager pager = (ViewPager) view.findViewById(R.id.pager); MyFragmentAdapter adapter = new MyFragmentAdapter(getFragmentManager(),images); pager.setAdapter(adapter); return view; } } 

这是非常简单的MyFragmentPagerAdapter,我只把getItem()方法,并且nullPointer检查:

 @Override public Fragment getItem(int position) { return MySimpleFragment.newInstance(images[position]); } 

最后SimpleFragment:

 public class SimpleFragment extends Fragment{ int id; public static SimpleAchFragment newInstance(int imgId){ SimpleFragment f = new SimpleFragment(); Bundle args = new Bundle(); args.putLong(ID_BUNDLE, imgId); f.setArguments(args); return f; } public SimpleAchFragment(){ } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.id = getArguments() != null ? getArguments().getInt(ID_BUNDLE) : 0; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View v = inflater.inflate(R.layout.simple_fragment, container, false); FragmentActivity mAct = getActivity(); ImageView img = (ImageView) v.findViewById(R.id.image); img.setImageDrawable(mAct.getResources().getDrawable(id)); return v; } } 

更多的信息,如果传递给适配器的内容(一个3个整数的int数组)的长度为零,则适配器不会尝试创build任何Fragment,因此对话框显示正确,但是为空(如预期的那样)。 在膨胀时抛出exception在SimpleFragment.onCreateView()。 exception中引用的id(未find)对应于ViewPager的id,在R.layout.simple_fragment中正确定义。

我也尝试使用AlertDialog.builder构build对话框,并直接使用Dialog()构造器,但是获得相同的行为。

Solutions Collecting From Web of "DialogFragment中的ViewPager + FragmentPagerAdapter获取“IllegalArgumentException:No view found …”"