ViewPager + FragmentPagerAdapter内DialogFragment获得"抛出:IllegalArgumentException:没有查看发现..." [英] ViewPager + FragmentPagerAdapter inside a DialogFragment gets "IllegalArgumentException:No view found..."

本文介绍了ViewPager + FragmentPagerAdapter内DialogFragment获得"抛出:IllegalArgumentException:没有查看发现..."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想表现出FragmentDialog(创建,并作为一个对话不添加为一个视图层次的内容显示),那里是一个ViewPager,其内容是由FragmentPagerAdapter给定(提供片段组成的图像)​​。

I am trying to show a FragmentDialog ( created and shown as a dialog NOT added as content in a view hierarchy) where there is a ViewPager whose content is given by a FragmentPagerAdapter (provides Fragments consisting of an image).

从FragmentActivity显示当ViewPager + FragmentPagerAdapter,但得到下面的异常从FragmentDialog这样做时,它的code运行完美

抛出:IllegalArgumentException:没有查看发现ID 0x7f040077的片段SimpleFragment ......

The code works perfect when showing ViewPager + FragmentPagerAdapter from a FragmentActivity, but get the following exception when doing it from a FragmentDialog:

下面是我的code:

通过一个按钮SherlockFragmentActivity创建和显示对话框。

Here is my code:

A SherlockFragmentActivity with a button to create and show the dialog.

在FragmentDialog:

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

The FragmentDialog:

这是很简单的MyFragmentPagerAdapter, 我把只为getItem()方法,空指针检查:

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

This is the very simple MyFragmentPagerAdapter, 
I put only the getItem() method, and nullPointer checks:

最后SimpleFragment:

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

And finally SimpleFragment:

更多信息,如果传递给适配器的内容(int数组有3个整数)的长度为零,则适配器不会尝试创建任何片段让对话框正常,但空出现时(如预期)。 唯一的例外是扔在SimpleFragment.onCreateView()在膨胀的时候。 在异常引用(如找不到)的ID对应ViewPager的ID,用正确的R.layout.simple_fragment定义。

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

我也试着与对话框()构造器建立对话与AlertDialog.builder,还可以直接,但得到相同的行为。

More info, if the content passed to the adapter ( an int array with 3 ints) has length zero, then the adapter doesn't try to create any Fragment so dialogs appears correctly but empty (as expected). The Exception is thrown at SimpleFragment.onCreateView() at the time of inflating. The id referenced in the exception (as not found) correspond to ViewPager 's id, with is properly defined in R.layout.simple_fragment.

推荐答案

试试这个:

在类的 AchGalleryDialog

In class AchGalleryDialog

而不是

MyFragmentAdapter adapter = new MyFragmentAdapter(getFragmentManager(),images); 

正因为如此

Because of this:

<一个href="http://developer.android.com/about/versions/android-4.2.html#NestedFragments">http://developer.android.com/about/versions/android-4.2.html#NestedFragments

希望这将帮助!

这篇关于ViewPager + FragmentPagerAdapter内DialogFragment获得&QUOT;抛出:IllegalArgumentException:没有查看发现...&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆