在自定义DialogFragment中找不到用于片段ID的视图

时间:2014-01-08 18:33:41

标签: android android-fragments android-viewpager

我知道那里有很多类似的问题,但我没有找到解决我问题的人,所以我们走了。

在一项活动中,我有这段代码:

TMTutorial tutorial = new TMTutorial();
tutorial.show(getSupportFragmentManager(), "blalba");

TMTutorial看起来像这样:

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import tm.mello.se.R;
import tm.mello.se.TMWelcomeFragment;

public class TMTutorial extends DialogFragment{
View mRootView;
LayoutInflater mInflater;

public TMTutorial() {

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    mRootView = inflater.inflate(R.layout.tutorial_layout, container);
    mInflater = inflater;

    setupViewPager();

    return mRootView;
}

private void setupViewPager()
{
    ViewPager viewPager = (ViewPager) mRootView.findViewById(R.id.tutorial_pager);

    viewPager.setAdapter(new Test(getFragmentManager()));

    //viewPager.setAdapter(new Test(getFragmentManager()));
}


public class Test extends FragmentPagerAdapter
{

    public Test(FragmentManager fm)
    {
        super(fm);
    }

    @Override
    public Fragment getItem(int i)
    {
        return new TMWelcomeFragment();
    }

    @Override
    public int getCount()
    {
        return 3;
    }
}
}
像这样的tutorial_layout.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent">

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

</FrameLayout>

我得到了这个堆栈跟踪:

 FATAL EXCEPTION: main
java.lang.IllegalArgumentException: No view found for id 0x7f05009d (xx.xxxx.xx:id/tutorial_pager) for fragment TMWelcomeFragment{42050698 #1 id=0x7f05009d android:switcher:2131034269:0}
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
        at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
        at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
        at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
        at android.view.View.measure(View.java:16030)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:314)
        at android.view.View.measure(View.java:16030)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:314)
        at android.view.View.measure(View.java:16030)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:314)
        at android.view.View.measure(View.java:16030)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:314)
        at android.view.View.measure(View.java:16030)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
        at android.view.View.measure(View.java:16030)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:314)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2196)
        at android.view.View.measure(View.java:16030)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1913)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1083)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1288)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1008)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5505)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
        at android.view.Choreographer.doCallbacks(Choreographer.java:562)
        at android.view.Choreographer.doFrame(Choreographer.java:532)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:213)
        at android.app.ActivityThread.main(ActivityThread.java:5225)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
        at dalvik.system.NativeStart.main(Native Method)

任何人都知道问题是什么? :S

3 个答案:

答案 0 :(得分:13)

您需要使用

viewPager.setAdapter(new Test(getChildFragmentManager()))

而不是

getSupportFragmentManager()

答案 1 :(得分:1)

链接上有片段生命周期:http://developer.android.com/images/fragment_lifecycle.png onCreateView完成后,将触发onActivityCreated方法。在onActivityCreated,您可以像这样获得viewPager: getView()findViewByIdR.id.tutorial_pager)。 而且您也没有必要返回新视图。你可以退货。

答案 2 :(得分:0)

在您的活动中尝试此操作:

Fragment fragment = TMTutorial.newInstance(); 
fragment.show(getSupportFragmentManager(), "blabla");

现在在TMTutorial中:

static TMTutorial newInstance() {
   TMTutorial tutorial = new TMTutorial();
   return tutorial;
}