当前位置: 首页 > 面试题库 >

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

罗源
2023-03-14
问题内容

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

在此对话框中,有一个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_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

问题答案:

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

唯一的主要区别是您应该使用a DialogFragment而不是a
Dialog,这样您就可以访问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;
    }

}

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_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>

dialog_fragment.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: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>

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_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>


 类似资料:
  • 我一整天都被困在这个问题上,我似乎找不到一个适合我的情况的答案,足以让我使用。 当我单击一个列表项时,比如Blue,在Portraint中,我希望它显示一个新的片段,这是一个蓝色的屏幕,而在Sandwork中,它在半个屏幕上显示该片段,在另一个屏幕上显示listview。我正在使用ActionBarSherlock和碎片来完成这一点。 我从里到外构建了它,所以我从一个列表开始,它按照我想要的方式工

  • 我找到的唯一解决办法就是打电话给 在显示父片段之后。这会以某种方式触发视图被重新创建并出现在屏幕上。不幸的是,这有时会导致在旧的观察器上处理两次调用的寻呼机适配器时出现异常。 有没有更好的办法做到这一点?我想我要问的是:

  • 我刚开始学Android。我有一个活动类,我想把它重定向到导航栏的ViewPage中的一个片段?我尝试了很多方法,但都不奏效。我希望能帮上忙!!!我的代码:有一个mainactivity.java有一个ViewPager和BottomNavigationView ViewPageApdater类 和Activity类,但我遇到一个问题:java.lang.IllegalArgumentExcept

  • 在我的应用程序中,我有标签的活动(让我们说10个标签)。每个选项卡页都包含和(此ListView中显示的数据是从我的服务器动态加载的)。我使用来显示这些页面。我不想将所有保存在内存中,所以我决定使用(我的适配器类扩展了这些类)。 > 假设选择了第三个选项卡。然后,当我转到第一个选项卡时,应该从头开始为该选项卡创建(这很好,这就是的工作方式),但不恢复该片段以前的状态(例如,不应该滚动到上次访问该时