Java 无法在表格布局中创建动态选项卡
我想在cardview中创建一个表格布局。但当我尝试动态添加选项卡时,会出现以下错误: 原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.support.design.widget.TabLayout$TabView.setContentDescription(java.lang.CharSequence)” 我已经搜索了很多stackoverflow帖子,但我无法解决这个问题。更重要的是,我没有appbarlayout,TableLayout不在工具栏的正下方,而是在cardview中 这是我的xml:Java 无法在表格布局中创建动态选项卡,java,android,android-fragments,nullpointerexception,Java,Android,Android Fragments,Nullpointerexception,我想在cardview中创建一个表格布局。但当我尝试动态添加选项卡时,会出现以下错误: 原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.support.design.widget.TabLayout$TabView.setContentDescription(java.lang.CharSequence)” 我已经搜索了很多stackoverflow帖子,但我无法解决这个问题。更重要的是,我没有appbarlayout
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/blue"
>
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/blue"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:id="@+id/toolbar"
>
</android.support.v7.widget.Toolbar>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="0dp"
app:cardCornerRadius="18dp"
android:elevation="10dp"
app:layout_constraintTop_toBottomOf="@id/toolbar"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<include
android:layout_margin="15dp"
layout="@layout/l1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
></include>
<include
layout="@layout/layout1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="130dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/l2"
></include>
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/l2"
android:id="@+id/tablayout"
app:tabGravity="fill"
app:tabMode="fixed"
></android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/tablayout"
android:id="@+id/viewPager"
>
</android.support.v4.view.ViewPager>
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
在这里,布局l1和l2是可见的,当我尝试动态创建选项卡时,它会使应用程序崩溃
以下是我的主要活动:
@BindView(R.id.toolbar)
Toolbar toolbar;
@BindView(R.id.tablayout)
TabLayout tabLayout;
@BindView(R.id.viewPager)
ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_xml);
ButterKnife.bind(this);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
List<String> title = new ArrayList<>();
title.add(TAB1TEXT);
title.add(TAB2TEXT);
title.add(TAB3TEXT);
title.add(TAB4TEXT);
final PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(),title);
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
tabLayout.addTab(new TabLayout.Tab().setText(TAB1TEXT));//CRASHES HERE
tabLayout.addTab(new TabLayout.Tab().setText(TAB2TEXT));
tabLayout.addTab(new TabLayout.Tab().setText(TAB3TEXT));
tabLayout.addTab(new TabLayout.Tab().setText(TAB4TEXT));
}
@BindView(R.id.toolbar)
工具栏;
@BindView(R.id.tablayout)
表格布局;
@BindView(R.id.viewPager)
查看页面查看页面;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_xml);
把(这个)绑起来;
设置支持操作栏(工具栏);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
列表标题=新建ArrayList();
标题.添加(表1文本);
title.add(TAB2TEXT);
标题.添加(表3文本);
标题.添加(TAB4TEXT);
最终PagerAdapter适配器=新PagerAdapter
(getSupportFragmentManager(),标题);
viewPager.setAdapter(适配器);
viewPager.addOnPageChangeListener(新建TabLayout.TabLayoutOnPageChangeListener(TabLayout));
tabLayout.setOnTabSelectedListener(新的tabLayout.OnTabSelectedListener(){
@凌驾
已选择的公共选项卡(TabLayout.Tab){
setCurrentItem(tab.getPosition());
}
@凌驾
已选择的公共选项卡(TabLayout.Tab){
}
@凌驾
已重新选择公共选项卡(TabLayout.Tab){
}
});
tabLayout.addTab(新tabLayout.Tab().setText(TAB1TEXT));//此处崩溃
tabLayout.addTab(新的tabLayout.Tab().setText(TAB2TEXT));
tabLayout.addTab(新的tabLayout.Tab().setText(TAB3TEXT));
tabLayout.addTab(新的tabLayout.Tab().setText(TAB4TEXT));
}
它实际上在这条线上崩溃了:
tabLayout.addTab(新建tabLayout.Tab().setText(TAB1TEXT))
Edit:tabLayout实例变量(崩溃发生的地方)不是null,我也不确定到底是什么导致了这个NPE,可能是内部的一些东西被抛出了强>
任何帮助或见解都将非常有用。提前感谢 不要使用@BindView,只需将全局变量设置为
TabLayout tabs;
然后,在@OnCreate after.setContentView()中
最好总是为视图中的每个元素指定一个变量,这些元素将更改或查看数据,这样做就可以了,活动无法找到特定的视图。
让我知道进展如何添加setContentDescription(“一些描述”);在TabView上。它再次崩溃,在同一个地方出现相同的异常!这里的tablayout实例不是空的,正因为这个原因,我才在这里发布了这个问题。此外,我已经清楚地指出了我在哪里实际面临这个错误!请在投票前看一看问题!我已经检查过很多次了,它不是空的,我移除了butterknife并使用了findviewbyid。但是它不工作!我道歉。我误读了异常消息。您不需要自己实例化
选项卡
s,而需要让TabLayout
来完成。也就是说,将newtablayout.Tab()
更改为TabLayout.newTab()
。例如,在崩溃行中:tabLayout.addTab(tabLayout.newTab().setText(TAB1TEXT))代码>。谢谢你的输入,我试过了,但它不工作“TAB1TEXT”来自哪里?这是上面声明的全局字符串
tabs = (TabLayout)findViewbyId(R.id.tablayout);