viewpager 中的片段,未找到视图错误

Posted

技术标签:

【中文标题】viewpager 中的片段,未找到视图错误【英文标题】:fragments in viewpager, no view found error 【发布时间】:2015-05-05 16:07:03 【问题描述】:

我有一个活动持有一个片段,在这个片段中有一个按钮,当它被点击时,会弹出一个对话框。

在这个对话框中,有一个 Viewpager,它包含一些要显示的片段。

这是代码和错误,请抽出宝贵的时间告诉我我错在哪里。非常感谢您的帮助。

MainActivity.class

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

public class MainActivity extends FragmentActivity 

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fm.beginTransaction();

    MyFragment fragment = new MyFragment();

    fragmentTransaction.add(R.id.container, fragment);

    fragmentTransaction.commit();



MyFragment.class

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class MyFragment extends Fragment 
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) 
    View v = inflater.inflate(R.layout.fragment_sandbox, container, false);
    Button button = (Button) v.findViewById(R.id.button);

    button.setOnClickListener(new OnClickListener() 

        @Override
        public void onClick(View arg0) 
            PagerDialog dialog = new PagerDialog(getActivity(),
                    getChildFragmentManager());
            dialog.show();
        
    );
    return v;


PagerDialog.class

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class PagerDialog extends Dialog 

ViewPager mViewPager;
FragmentStatePagerAdapter mAdapter;
FragmentManager mFragmentManager;

public PagerDialog(Context context, FragmentManager fm) 
    super(context);
    mFragmentManager = fm;


@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dialog);
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mAdapter = new MyAdapter(mFragmentManager);
    mViewPager.setAdapter(mAdapter);



private class MyAdapter extends FragmentStatePagerAdapter 

    public MyAdapter(FragmentManager fm) 
        super(fm);

    

    @Override
    public Fragment getItem(int index) 
        return new DummyFragment();
    

    @Override
    public int getCount() 
        return 2;
    



private class DummyFragment extends Fragment 
    @Override
    public View onCreateView(LayoutInflater inflater,
            @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState) 

        View v = inflater.inflate(R.layout.fragment_dummy_layout,
                container, false);
        return v;
    


这里是 dialog.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_
    android:orientation="vertical" >

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_
    android:layout_ />

</LinearLayout>

这是错误

03-06 19:43:38.487: E/AndroidRuntime(1167): FATAL EXCEPTION: main
03-06 19:43:38.487: E/AndroidRuntime(1167): Process: com.me.sandbox, PID: 1167
03-06 19:43:38.487: E/AndroidRuntime(1167): java.lang.IllegalArgumentException: No view found for id 0x7f05003d (com.mochimira.sandbox:id/pager) for fragment DummyFragmentb2d9f8c8 #0 id=0x7f05003d
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:939)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at android.view.View.measure(View.java:16497)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at android.view.View.measure(View.java:16497)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at android.view.View.measure(View.java:16497)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at android.view.View.measure(View.java:16497)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at android.view.View.measure(View.java:16497)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at android.view.View.measure(View.java:16497)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at android.view.Choreographer.doCallbacks(Choreographer.java:574)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at android.view.Choreographer.doFrame(Choreographer.java:544)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at android.os.Handler.handleCallback(Handler.java:733)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at android.os.Handler.dispatchMessage(Handler.java:95)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at android.os.Looper.loop(Looper.java:136)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at android.app.ActivityThread.main(ActivityThread.java:5017)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at java.lang.reflect.Method.invokeNative(Native Method)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at java.lang.reflect.Method.invoke(Method.java:515)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-06 19:43:38.487: E/AndroidRuntime(1167): 	at dalvik.system.NativeStart.main(Native Method

【问题讨论】:

我问的方式有什么问题吗?我没有你的答案。 嗯,不,我看不出你的问题有什么问题。但我没有看到明显的错误。问题似乎是您的对话框找不到的 ViewPager。您可以尝试将 Button 或 TextView 添加到 Dialog 而不是 Pager 吗? 在logcat中双击此错误行,将您带到导致此错误的行,然后在此处发布该行代码 @Deutro:我照你说的做了。我向对话框添加了一个按钮,而不是向对话框添加了寻呼机,它正常显示。没问题。你是对的,似乎是 ViewPager 无法被 Dialog 找到,但我不明白为什么。 @abforce:我已经更新了日志中的错误,请查看,当我双击错误行时。我无法进入代码,因为它在 android API 中并且它没有显示在我的 Eclipse 中。 【参考方案1】:

我找到了问题的解决方案并修改了您的类,以便不再发生此错误。

唯一的主要区别是您应该使用DialogFragment 而不是Dialog,这样您就可以调用getChildFragmentManager() 并从DialogFragment 接收正确的FragmentManager

即使您之前使用过getChildFragmentManager(),它也来自MyFragment,并且PagerDialog 类不是MyFragment 中的子片段。

我已经测试了下面的代码,现在应该可以正常工作了。

我的片段

public class MyFragment extends Fragment 
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
        View v = inflater.inflate(R.layout.fragment_sandbox, container, false);
        Button button = (Button) v.findViewById(R.id.button);

        button.setOnClickListener(new OnClickListener() 

            @Override
            public void onClick(View arg0) 
                DialogFragment newFragment = PagerDialog.newInstance();
                newFragment.show(getChildFragmentManager(), "dialog");
            

        );

        return v;
    

PagerDialog

public class PagerDialog extends DialogFragment 

    public static PagerDialog newInstance() 
        return new PagerDialog();
    

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
        View v = inflater.inflate(R.layout.dialog_fragment, container, false);

        ViewPager mViewPager = (ViewPager) v.findViewById(R.id.view_pager);

        /* Use childFragmentManager here provided from the PagerDialog */
        MyAdapter mAdapter = new MyAdapter(getChildFragmentManager());
        mViewPager.setAdapter(mAdapter);

        return v;
    

    private class MyAdapter extends FragmentStatePagerAdapter 

        public MyAdapter(FragmentManager fm) 
            super(fm);
        

        @Override
        public Fragment getItem(int index) 
            return new DummyFragment();
        

        @Override
        public int getCount() 
            return 2;
        

    

虚拟片段

public class DummyFragment extends Fragment 

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 

        View v = inflater.inflate(R.layout.fragment_dummy_layout, container, false);
        return v;
    


fragment_sandbox.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_
    android:layout_
    android:gravity="center">

    <Button
        android:id="@+id/button"
        android:layout_
        android:layout_
        android:text="Test Fragment Dialog Pager"
        android:textSize="24sp"
        android:padding="20dp" />

</LinearLayout>

dialog_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:layout_
        android:layout_
        android:text="Fragment Dialog Title Text "
        android:padding="10dp"
        android:textColor="#333"
        android:textSize="24sp"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_
        android:layout_/>

</LinearLayout>

fragment_dummy_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_
    android:layout_
    android:gravity="center">

    <TextView
        android:layout_
        android:layout_
        android:text="Fragment Dummy Text"
        android:textSize="24sp"
        android:textColor="#ff0000"/>

</LinearLayout>

【讨论】:

非常感谢,稍后我会测试代码并反馈。 我已经测试了代码,完美,现在我明白了。谢谢!【参考方案2】:

我在 Google 上找到了一篇博文,上面说 viewpager 在 Dialog 上不起作用。 它还说我们应该改用 DialogFragment。

这是该博客的链接:http://www.intellicode.in/viewpager-inside-dialog/

【讨论】:

感谢您的回答,我已将 ViewPager 替换为 Fragment 并且出现相同的错误:“未找到视图”。总结一下我的代码中的问题,ViewPager 不能在 Dialog 上工作,而且 Fragment 也不能​​在 Dialog 上工作。直觉上,对我来说,嵌套片段似乎有问题,但我无法弄清楚。【参考方案3】:

getChildFragmentManager() 在您使用 v4 支持库时从 API 17 开始可用。尝试改用支持片段管理器:

PagerDialog dialog = new PagerDialog(getActivity(),
        getActivity().getSupportFragmentManager());

【讨论】:

【参考方案4】:

Dialog 类是对话框的基类,但应避免直接实例化 Dialog。相反,请使用以下子类之一:

AlertDialog 一个可以显示标题、最多三个按钮、可选项目列表或自定义布局的对话框。还有 DatePickerDialog 或 TimePickerDialog。

这些类定义了对话框的样式和结构,但您应该使用 DialogFragment 作为对话框的容器。 DialogFragment 类提供了创建对话框和管理其外观所需的所有控件,而不是调用 Dialog 对象上的方法。

更多详情请到Dialog Design

【讨论】:

【参考方案5】:

错误听起来像是在 DummyFragment (fragment_dummy_layout.xml) 的 xml 文件中寻找 id 寻呼机。我想知道这是否是由于在与setContentView(R.layout.dialog); 相同的文件中调用了View v = inflater.inflate(R.layout.fragment_dummy_layout, container, false);。您是否尝试过将 DummyFragment 分离到它自己的文件中?

你也可以试试

View v = getActivity().getLayoutInflater().inflate(R.layout.fragment_dummy_layout, 
        container, false);

【讨论】:

感谢您的帮助!【参考方案6】:

尝试使用 DialogFragment 并将 getChildFragmentManager() 传递给 FragmentPagerAdapter 的 构造函数。

对话框片段:

public static class MyDialogFragment extends DialogFragment 

    private ViewPager viewPager;
    MySectionPagerAdapter mAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
        View view = inflater.inflate(R.layout.m_layout, container);

        viewPager = (ViewPager) view.findViewById(R.id.viewpager);
        mAdapter = new MySectionPagerAdapter(getChildFragmentManager());
        viewPager.setAdapter(mAdapter);

        return view;
    



【讨论】:

【参考方案7】:

到目前为止,我们无法弄清楚为什么会出现错误,但我有办法解决这个问题,使用以下提示:

提示:如果您想要自定义对话框,则可以将 Activity 显示为对话框,而不是使用对话框 API。只需创建一个活动并将其主题设置为清单元素中的 Theme.Holo.Dialog:

<activity android:theme="@android:style/Theme.Holo.Dialog"

根据这个技巧,我不会让 PagerDialog 扩展 Dialog 而是从 Fragment 扩展。然后将这个 Fragment 放入一个 Activity 中,在 AndroidManifest.xml 中设置这个 Activity 的主题如上。

PagerDialog 的更新代码如下。

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class PagerDialog extends Fragment 

ViewPager mViewPager;
FragmentStatePagerAdapter mAdapter;

@Override
public View onCreateView(LayoutInflater inflater,
        @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) 
    View v = inflater.inflate(R.id.dialog, container, false);
    mViewPager = (ViewPager) v.findViewById(R.id.pager);
    mAdapter = new MyAdapter(getFragmentManager());
    mViewPager.setAdapter(mAdapter);
    return v;


private class MyAdapter extends FragmentStatePagerAdapter 

    public MyAdapter(FragmentManager fm) 
        super(fm);

    

    @Override
    public Fragment getItem(int index) 
        return new DummyFragment();
    

    @Override
    public int getCount() 
        return 2;
    



private class DummyFragment extends Fragment 
    @Override
    public View onCreateView(LayoutInflater inflater,
            @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState) 

        View v = inflater.inflate(R.layout.fragment_dummy_layout,
                container, false);
        return v;
    


如果您有更好的解决方案,或者您可以找出我问题中的原始代码为什么不起作用,请教我。我是 Android 编程的新手,很高兴向大家学习。

干杯。

【讨论】:

【参考方案8】:

我想你用过

Dialog dialog = new Dialog(context);

支持这个。

第一步:使用DialogFragment

public class MyCustomDialog extends DialogFragment 
    Button mButton;
    EditText mEditText;


    List<MasterPageModel> listdata;
    int position;
    ViewPager viewpager;

    public MyCustomDialog(List<MasterPageModel> listdata, int position) 
        this.listdata = listdata;
        this.position = position;
    




    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
        final View dialog = inflater.inflate(R.layout.dailoglayout, container);

        final Dialog dailog = getDialog();
        dailog.getWindow().setLayout(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        dailog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        dailog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        ImageView closebtn = (ImageView) dialog.findViewById(R.id.closeimgeview);
        closebtn.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                dailog.dismiss();
            
        );

        viewpager = (ViewPager) dialog.findViewById(R.id.dailog_viewpager);

        **viewpager.setAdapter(new PagerAdapter(getChildFragmentManager(), listdata));** //This Line is Very important
        viewpager.setCurrentItem(position);

        return dialog;
    


    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen); //For Full Screen of DailogBox
    

第二步:设置FragmentViewPager

public class PagerAdapter extends FragmentPagerAdapter   

    private final List<MasterPageModel> pages;

    public PagerAdapter(FragmentManager fm, List<MasterPageModel> pages) 
        super(fm);
        this.pages = pages;
    



    @Override
    public Fragment getItem(int position) 
        return new ViewPagerFragment(pages.get(position));
    

    @Override
    public int getCount() 
        return pages.size();
    

第三步:ViewPager 的片段视图

public class ViewPagerFragment extends Fragment 

    MasterPageModel masteDetailModel;


    public ViewPagerFragment(MasterPageModel questionItem) 
        this.masteDetailModel = questionItem;
    

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
        View v = inflater.inflate(R.layout.pagerlayout, container, false);

        ImageView image = (ImageView) v.findViewById(R.id.pager_imageview);
        Log.i("Image", "=>" + masteDetailModel.imagelarge);
        Picasso.with(getActivity()).load(masteDetailModel.imagethumb).error(R.drawable.img).into(image);

        return v;
    

第四个如何使用这个对话片段

 MyCustomDialog fragment1 = new MyCustomDialog(listdata, position);
                EditionDetailActivity act = (EditionDetailActivity) context;
                FragmentManager fm = act.getSupportFragmentManager();
                fragment1.show(fm, "");

【讨论】:

以上是关于viewpager 中的片段,未找到视图错误的主要内容,如果未能解决你的问题,请参考以下文章

由于未找到 ID 错误的视图,列表片段未出现

如何确定 Fragment 何时在 ViewPager 中可见

无法从 ViewPager 中的另一个片段刷新/更新片段中的列表视图

Android ViewPager,片段中的cameraview未显示

片段中的自定义列表视图。未找到布局

片段对话框中的片段膨胀引发错误“片段未创建视图”