我正在尝试在 android 中制作可滑动的选项卡布局,但在操作栏中获得空值
I am trying to make the swipeable tab layout in android but getting null value in action bar
我正在尝试为 {profile} 和 {Child} 制作两个标签布局,它们也应该可以滑动。我已经为它们创建了布局和片段,并且在 Main Activity 布局上,我已经放置了
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
在我的 Main Activity.java class 中,我的代码是
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = {"Profile", "Child"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
// Initialization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = (ActionBar) getSupportActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
但在 actionBar 中,我得到的是空值,因此程序在该点执行时使用空指针。由于什么原因,null 可能会出现在 getActionBar 中。
将您的 activity 主题更改为 @style/Theme.AppCompat
或 在布局中定义工具栏:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
并设置自定义操作栏:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
Toolbar toolbar = (Toolbar) findViewById(toolbar);
setSupportActionBar(toolbar);
actionBar = (ActionBar) getSupportActionBar();
}
ActionBar
选项卡已弃用,自 API 21 起无法使用。您应该使用 PagerSlidingTabStrip 库。
我正在尝试为 {profile} 和 {Child} 制作两个标签布局,它们也应该可以滑动。我已经为它们创建了布局和片段,并且在 Main Activity 布局上,我已经放置了
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
在我的 Main Activity.java class 中,我的代码是
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = {"Profile", "Child"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
// Initialization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = (ActionBar) getSupportActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
但在 actionBar 中,我得到的是空值,因此程序在该点执行时使用空指针。由于什么原因,null 可能会出现在 getActionBar 中。
将您的 activity 主题更改为 @style/Theme.AppCompat
或 在布局中定义工具栏:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
并设置自定义操作栏:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
Toolbar toolbar = (Toolbar) findViewById(toolbar);
setSupportActionBar(toolbar);
actionBar = (ActionBar) getSupportActionBar();
}
ActionBar
选项卡已弃用,自 API 21 起无法使用。您应该使用 PagerSlidingTabStrip 库。