为应用程序介绍创建滑动片段

时间:2016-01-03 15:42:00

标签: java android android-fragments

我有一个项目,我正在制作可刷卡的介绍,用户基本上刷了四页,将他/她介绍给应用程序并解释其功能等。但是,我不断收到以下错误:

String source = "\"car\"";
String utf16base64 = new String(Base64.getEncoder().encode(source.getBytes("UTF-16LE")));

我的项目组织如下:

enter image description here

我有我的IntroActivity:

 android.content.res.Resources$NotFoundException: Resource ID #0x0
 at android.content.res.Resources.getValue(Resources.java:1351)
 at android.content.res.Resources.loadXmlResourceParser(Resources.java:2774)
 at android.content.res.Resources.getLayout(Resources.java:1165)
 at android.view.LayoutInflater.inflate(LayoutInflater.java:421)
 at com.scyon.gradebook.WelcomeFragment.onCreateView(WelcomeFragment.java:40)
 at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
 at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
 at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
 at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
 at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
 at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
 at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
 at android.support.v4.view.ViewPager.populate(ViewPager.java:1106)
 at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
 at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1474)
 at android.view.View.measure(View.java:18788)
 at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
 at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:610)
 at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:677)
 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.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
 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.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 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:1216)
 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.Zygote

最后我有了WelcomeFragment模型类:

public class IntroActivity extends AppCompatActivity {

private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_feature);

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

}

public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int position) {

        if (position == 0) {

            return new WelcomeFragment().newInstance("testtitle#1",R.layout.fragment_welcome_one,R.id.welcome1);

        }else if (position == 1){

            return new WelcomeFragment().newInstance("testtitle#2", R.layout.fragment_welcome_two, R.id.welcome2);

        }else if (position == 2){

            return new WelcomeFragment().newInstance("testtitle#3", R.layout.fragment_welcome_three, R.id.welcome3);

        }else if (position == 3){

            return new WelcomeFragment().newInstance("testtitle#4", R.layout.fragment_welcome_four, R.id.welcome4);

        }

        return null;
    }

    @Override
    public int getCount() {
        return 4;
    }

}
}

任何帮助将不胜感激。我已经多次浏览了我的代码,我发现资源ID被认为是空的,但我不明白为什么会这样。

1 个答案:

答案 0 :(得分:2)

问题在于您除了newInstance()之外还调用了默认构造函数,因此您要连续创建两个片段,而第一个片段没有布局ID

使newInstance()方法保持静态,这样您就不必创建虚拟对象来调用newInstance()方法:

public static WelcomeFragment newInstance(String title, int layoutID, int titleID) {

    WelcomeFragment mWelcomeFragment = new WelcomeFragment();

    mTitle = title;
    mLayoutID = layoutID;
    mTitleID = titleID;

    return mWelcomeFragment;
}

然后,将其称为静态方法,而不是调用new WelcomeFragment()

@Override
public Fragment getItem(int position) {

    if (position == 0) {

        return WelcomeFragment.newInstance("testtitle#1",R.layout.fragment_welcome_one,R.id.welcome1);

    }else if (position == 1){

        return WelcomeFragment.newInstance("testtitle#2", R.layout.fragment_welcome_two, R.id.welcome2);

    }else if (position == 2){

        return WelcomeFragment.newInstance("testtitle#3", R.layout.fragment_welcome_three, R.id.welcome3);

    }else if (position == 3){

        return WelcomeFragment.newInstance("testtitle#4", R.layout.fragment_welcome_four, R.id.welcome4);

    }

    return null;
}