gpt4 book ai didi

java - 为应用介绍创建滑动 fragment

转载 作者:行者123 更新时间:2023-11-29 20:12:31 26 4
gpt4 key购买 nike

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

 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

我的项目组织如下:

enter image description here

我有我的 IntroActivity:

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

}
}

最后我有了我的 WelcomeFragment 模型类:

public class WelcomeFragment extends Fragment {

String mTitle;
int mLayoutID;
int mTitleID;

public WelcomeFragment() {

}

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

WelcomeFragment mWelcomeFragment = new WelcomeFragment();

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

return mWelcomeFragment;
}

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

View rootView = inflater.inflate(mLayoutID,container,false);
TextView textView = (TextView)rootView.findViewById(mTitleID);
Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Roboto-Thin.ttf");
textView.setText(mTitle);
textView.setTypeface(font);

return rootView;
}
}

如有任何帮助,我们将不胜感激。我检查了我的代码好几次,我发现资源 ID 应该是空的,但我不明白为什么会这样。

最佳答案

问题在于,除了 newInstance() 之外,您还调用了默认构造函数,因此您连续创建了两个 Fragment,而第一个没有布局 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;
}

关于java - 为应用介绍创建滑动 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34578328/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com