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

问题描述

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

唯一的主要区别是您应该使用a DialogFragment而不是aDialog,这样您就可以访问getChildFragmentManager()FragmentManager从接收正确的信息DialogFragment

即使您以前使用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;
    }
}

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

}

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

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

</LinearLayout>

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        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_width="match_parent"
        android:layout_height="200dp"/>

</LinearLayout>

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

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

</LinearLayout>

解决方法

我有一个活动,其中包含一个片段,在该片段中有一个按钮,当单击它时,会弹出一个对话框。

在此对话框中,有一个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,false);
        return v;
    }
}
}

这是dialog.xml:

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

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

</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 DummyFragment{b2d9f8c8 #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

猜你在找的技术问答相关文章

如何检查配对的蓝牙设备是打印机还是扫描仪(Android)
是否允许实体正文进行HTTP DELETE请求?
如何将ZipInputStream转换为InputStream?
java.util.logging Java 8中的变量
PowerMockito.doReturn返回null
Java中的RESTful调用
Swing / Java:如何正确使用getText和setText字符串
特殊字符和重音字符
Android Studio中的ndk.dir错误
错误“找不到主类”