没有为片段 OneFragment92c0220 #1 id=0x7f0e00d5 android:switcher:2131624149:0 找到 id 0x7f0e00d5 (com.exampl
Posted
技术标签:
【中文标题】没有为片段 OneFragment92c0220 #1 id=0x7f0e00d5 android:switcher:2131624149:0 找到 id 0x7f0e00d5 (com.example.page:id/dialogViewpager) 的视图【英文标题】:No view found for id 0x7f0e00d5 (com.example.page:id/dialogViewpager) for fragment OneFragment92c0220 #1 id=0x7f0e00d5 android:switcher:2131624149:0没有为片段 OneFragment92c0220 #1 id=0x7f0e00d5 android:switcher:2131624149:0 找到 id 0x7f0e00d5 (com.example.page:id/dialogViewpager) 的视图 【发布时间】:2017-05-07 20:09:42 【问题描述】:我想在对话框中显示 tabview。 我有一个主类文件,它扩展到 FragmentActivity,在里面我通过调用下面给出的这个方法来显示对话框。
private void dialogDictionary()
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_dictionary);
dialog.setCancelable(true);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
ImageView imgDialogDictionaryClose = (ImageView) dialog.findViewById(R.id.imgDialogDictionaryClose);
imgDialogDictionaryClose.setOnClickListener(v -> dialog.dismiss());
ViewPager viewPager = (ViewPager) dialog.findViewById(R.id.dialogViewpager);
setupViewPager(viewPager);
TabLayout tabLayout = (TabLayout) dialog.findViewById(R.id.dialogTabs);
tabLayout.setupWithViewPager(viewPager);
dialog.show();
private void setupViewPager(ViewPager viewPager)
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new OneFragment(), "ONE");
adapter.addFragment(new TwoFragment(), "TWO");
adapter.addFragment(new ThreeFragment(), "THREE");
viewPager.setAdapter(adapter);
class ViewPagerAdapter extends FragmentPagerAdapter
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager)
super(manager);
@Override
public Fragment getItem(int position)
return mFragmentList.get(position);
@Override
public int getCount()
return mFragmentList.size();
public void addFragment(Fragment fragment, String title)
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
@Override
public CharSequence getPageTitle(int position)
return mFragmentTitleList.get(position);
当我运行应用程序时,我收到此错误。
E/FragmentManager: No view found for id 0x7f0e00d5 (com.example.page:id/dialogViewpager) for fragment OneFragmenteb2a3cc #1 id=0x7f0e00d5 android:switcher:2131624149:0
E/FragmentManager: Activity state:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.page, PID: 2710
java.lang.IllegalArgumentException: No view found for id 0x7f0e00d5 (com.example.page:id/dialogViewpager) for fragment OneFragmenteb2a3cc #1 id=0x7f0e00d5 android:switcher:2131624149:0
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1107)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1295)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1643)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:679)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1272)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1120)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1646)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
at android.view.View.measure(View.java:18788)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1191)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
dialog_dictionary.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:layout_margin="20dp"
android:orientation="vertical">
<RelativeLayout
android:layout_
android:layout_
android:background="@drawable/dialog_title_bar">
<View
android:layout_
android:layout_
android:focusableInTouchMode="true" />
<EditText
android:id="@+id/edtDictionarySearch"
android:layout_
android:layout_
android:layout_centerVertical="true"
android:layout_marginLeft="25dp"
android:layout_marginRight="10dp"
android:layout_toLeftOf="@+id/imgDialogDictionaryClose"
android:background="@android:color/transparent"
android:drawableLeft="@mipmap/icn_search_white"
android:drawablePadding="10dp"
android:hint="@string/search"
android:singleLine="true"
android:textColor="@color/white"
android:textColorHint="@color/white"
android:textSize="@dimen/dialog_search_text_size" />
<ImageButton
android:id="@+id/imgDialogDictionaryClose"
android:layout_
android:layout_
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:background="@android:color/transparent"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:src="@mipmap/icn_close" />
</RelativeLayout>
<android.support.design.widget.TabLayout
android:id="@+id/dialogTabs"
android:layout_
android:layout_
app:tabGravity="fill"
app:tabMode="fixed" />
<RelativeLayout
android:layout_
android:layout_
android:background="@drawable/dialog_bg_area"
android:padding="@dimen/dialog_inner_padding">
<android.support.v4.view.ViewPager
android:id="@+id/dialogViewpager"
android:layout_
android:layout_
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</RelativeLayout>
</LinearLayout>
OneFragment.java
public class OneFragment extends Fragment
public OneFragment()
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
return inflater.inflate(R.layout.fragment_one, container, false);
fragment_one.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:orientation="vertical">
<TextView
android:layout_
android:layout_
android:gravity="center"
android:text="Tab One"
android:textSize="50dp" />
</LinearLayout>
【问题讨论】:
检查视图寻呼机的 id ...你是否使用了正确的 id ??? 是的,我检查了它是否正确 重建你的项目...这将重新创建 R 文件 我也重建了 3 次以上,清理构建了 10 多次 你能显示layout/dialog_dictionary的xml吗??? 【参考方案1】:据我所知,Dialog 的内容视图不是立即添加的。它是在显示对话框时添加的。您可以尝试做的是创建 Dialog 的子类并将全局布局侦听器添加到您的 Dialog 子类。
当 GlobalLayoutListener 触发时,此时获取您的引用,然后删除 Global LayoutListener,这样它就不会继续重新触发。
// in OnCreate()
getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener()
@Override
public void onGlobalLayout()
//get references for viewPager
viewPager = findViewById(R.id.dialogViewpager)
//remove listener so it doesn't fire again
getViewTreeObserver().removeOnGlobalLayoutListener(this);
);
更新:
实际上,按照我的想法,您可以创建一个普通对话框并将布局侦听器添加到其中
//note that dialog will need to be final to access it
//in onGlobalLayout below or have a global reference in your class
dialog.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener()
@Override
public void onGlobalLayout()
//get references for viewPager
viewPager = findViewById(R.id.dialogViewpager)
//remove listener so it doesn't fire again
dialog.getViewTreeObserver().removeOnGlobalLayoutListener(this);
);
【讨论】:
你将它应用于什么元素?它需要应用于视图元素。因此,如果您是子类化,请将其添加到视图层次结构中的根元素 我正在申请Dialog
【参考方案2】:
看起来不错...here google 说要避免直接实例化 Dialog ...使用它的子类...我认为您正在尝试做的事情可以通过 DialogFragment... 希望它会有所帮助
【讨论】:
以上是关于没有为片段 OneFragment92c0220 #1 id=0x7f0e00d5 android:switcher:2131624149:0 找到 id 0x7f0e00d5 (com.exampl的主要内容,如果未能解决你的问题,请参考以下文章
如果没有可用的互联网,如何将 ContentView 设置为另一个片段,如果连接可用,如何恢复片段
java.lang.IllegalArgumentException:没有为片段的 id 0x1020002 (android:id/content) 找到视图