活动类:
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 java.lang.IllegalStateException:带适配器的ViewPager com.example.locale.tablayoutdynamicsample.ViewPagerAdapter@3b0e90d6需要一个视图id 在android.support.v4.app.FragmentStatePagerAdapter.startUpdate(FragmentStatePagerAdapter.java:87) 在android.support.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) 在android.view.View.measure(View.java:18580) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827) 在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1435) 在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) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:430) 在android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139) 在android.view.View.measure(View.java:18580) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827) 在android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:393) 在android.view.View.measure(View.java:18580) 在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) 在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1435) 在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) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:430) 在com.android.internal.policy.impl.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:2897) 在android.view.View.measure(View.java:18580) 在android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2248) 在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1306) 在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1548) 在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1191) 在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6642) 在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:777) 在android.view.Choreographer.doCallbacks(Choreographer.java:590) 在android.view.Choreographer.doFrame(Choreographer.java:560) 在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:763) 在android.os.Handler.handleCallback(Handler.java:739) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:145) 在android.app.ActivityThread.main(ActivityThread.java:5951) at java.lang.reflect.Method.invoke(Native Method) 在java.lang.reflect.Method.invoke(Method.java:372) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1388) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
答案 0 :(得分:0)
它说" VewPager with Adapter ViewPagerAdapter@3b0e90d6 requires a view id
"所以试试这个:
viewPager.setId(anyInteger);