Tablayout与Viewpager编程创建不工作
活动类别:Tablayout与Viewpager编程创建不工作
public class DynamicActivity extends AppCompatActivity{ private LinearLayout mLinearLayout;
private View mView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLinearLayout = new LinearLayout(DynamicActivity.this);
mLinearLayout.setOrientation(LinearLayout.VERTICAL);
mLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
TabLayout tabLayout = new TabLayout(DynamicActivity.this);
tabLayout.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setBackgroundColor(Color.CYAN);
mLinearLayout.addView(tabLayout);
ViewPager viewPager = new ViewPager(DynamicActivity.this);
viewPager.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
viewPager.setBackgroundColor(Color.BLUE);
mLinearLayout.addView(viewPager);
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(viewPagerAdapter);
tabLayout.addTab(tabLayout.newTab());
tabLayout.addTab(tabLayout.newTab());
tabLayout.addTab(tabLayout.newTab());
tabLayout.setupWithViewPager(viewPager);
setContentView(mLinearLayout);
}
}
**Adapter Class :**
public class ViewPagerAdapter extends FragmentStatePagerAdapter{
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public CharSequence getPageTitle(int position) {
return "Test";
}
@Override
public Fragment getItem(int position) {
return new TestFagment();
}
@Override
public int getCount() {
return 3;
}
}
例外:
致命异常:主
E/AndroidRuntime:致命异常:主 过程: com.example.locale.tablayoutdynamicsample,PID:27084 JAV a.lang.IllegalStateException:具有适配器的ViewPager [email protected]0d6需要在android.support上的android.support.v4.app.FragmentStatePagerAdapter.startUpdate(FragmentStatePagerAdapter.java:87) 处的视图ID为 。 v4.view.ViewPager.populate(ViewPager.java:1148) 在android.support.v4.view.ViewPager.populate(ViewPager.java:1116) 在android.support.v4.view.ViewPager.onMeasure(ViewPager。 java:1642) at android.view.View.measure(View.java:18580) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827) at android.widget.LinearLayout.measureChildBeforeLayout(L inearLayout.java:1435) 在android.widget.LinearLayout.measureVertical(LinearLayout.java:721) 在android.widget.LinearLayout.onMeasure(LinearLayout.java:612) 在android.view.View.measure(查看。 java:18580) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827) at android.widget.FrameLayout.onMeasure(FrameLayout.java:430) at android.support.v7.widget.ContentFrameLayout.onMeasure( ContentFrameLayout.java:139) 在android.view.View.measure(View.java:18580) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827) at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:393) at android.view.View.measure(View.java:18580) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java: 5827) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:430) 在android.view.View.measure(View.java:18580) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1435) at android.widget.LinearLayout.measureVertical(LinearLayout.java:721) 在android.widget.LinearLayout.onMeasure(LinearLayout.java:612) 在android.view.View.measure(View.java:18580) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827) 在机器人.widget.FrameLayout.onMeasure(FrameLayout.java:430) at com.android.internal.policy.impl.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:2897) at android.view.View.measure(View.java :18580) at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2248) at android.view.View.RootImpl.measureHierarchy(ViewRootImpl.java:1306) at android.view。ViewRootImpl.performTraversals(ViewRootImpl.java:1548) 在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1191) 在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6642) 在android.view。编排$ CallbackRecord.run(Choreographer.java:777) at android.view.Choreographer.doCallbacks(Choreographer.java:590) at android.view.Choreographer.doFrame(Choreographer.java:560) at android.view。编排$ FrameDisplayEventReceiver.run(Choreographer.java:763) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dis patch.Message(Handler.java:95) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5951) at java.lang.reflect.Method。调用(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1388) at com.android .internal.os.ZygoteInit.main(ZygoteInit.java:1183)
回答:
它说: “VewPager with Adapter [email protected] requires a view id
” 这样试试这个:
viewPager.setId(anyInteger);
以上是 Tablayout与Viewpager编程创建不工作 的全部内容, 来源链接: utcz.com/qa/262478.html