Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我试图在android中创建swipeable选项卡布局,但在操作栏中得到空值_Java_Android_Android Fragments_Tabs_Swipe - Fatal编程技术网

Java 我试图在android中创建swipeable选项卡布局,但在操作栏中得到空值

Java 我试图在android中创建swipeable选项卡布局,但在操作栏中得到空值,java,android,android-fragments,tabs,swipe,Java,Android,Android Fragments,Tabs,Swipe,我正在尝试为{profile}和{Child}创建两个选项卡布局,它们也应该是可切换的。我已经为它们创建了布局和片段,在主要活动布局上,我已经将 <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_heigh

我正在尝试为{profile}和{Child}创建两个选项卡布局,它们也应该是可切换的。我已经为它们创建了布局和片段,在主要活动布局上,我已经将

<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">

但在actionBar中,我得到了空值,所以程序在那个点用空指针执行。由于什么原因,getActionBar中可能出现null。

将活动主题更改为
@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>

ActionBar
选项卡已弃用,自API 21以来无法使用。您应该使用库。

将活动主题更改为
@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>


ActionBar
选项卡已弃用,自API 21以来无法使用。您应该使用library。

我已经尝试过actionBar=getActionBar();这也是。但空指针异常的结果相同。您的活动有什么主题?您用于活动的主题是什么?如果您使用类似“Theme.AppCompat.Light.NoActionBar”,则需要将工具栏添加到布局中。我使用的是android:Theme=“@style/Theme.AppCompat.NoActionBar”。现在我需要做什么才能让它工作呢?嗨,Onur Taskin,在我的清单中,主题是:android:theme=“@style/theme.AppCompat.NoActionBar”,我已经用actionBar=getActionBar()尝试过我的xml布局;这也是。但空指针异常的结果相同。您的活动有什么主题?您用于活动的主题是什么?如果您使用类似“Theme.AppCompat.Light.NoActionBar”,则需要将工具栏添加到布局中。我使用的是android:Theme=“@style/Theme.AppCompat.NoActionBar”。现在我需要做什么才能让它工作呢?嗨,Onur Taskin,在我的清单中,主题是:android:theme=“@style/theme.AppCompat.NoActionBar”并且我的xml布局有Hi Mixel,我尝试了这个@Override protected void onCreate(Bundle savedInstanceState){//getWindow().requestFeature(Window.FEATURE\u ACTION\u BAR);super.onCreate(savedInstanceState);setContentView(R.layout.activity_edit);//初始化viewPager=(viewPager)findViewById(R.id.pager);工具栏工具栏=(Toolbar)findViewById(R.id.Toolbar);//setSupportActionBar(工具栏);actionBar=(actionBar)getSupportActionBar(工具栏);但是通过
Toolbar
无法解析getSupportActionBar“您是指
android.support.v7.widget.Toolbar
还是
android.widget.Toolbar
?您必须先使用。嗨,Mixel,我使用了android.support.v7.widget.Toolbar;您应该调用
getSupportActionBar()
在我的回答中没有参数。@mixel:我使用了`viewPager=(viewPager)findViewById(R.id.pager);Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar);setSupportActionBar(Toolbar);actionBar=getActionBar()`我犯了错误。因为我在actionBarHi Mixel中仍然得到null,我尝试了这个@Override protected void onCreate(Bundle savedInstanceState){//getWindow().requestFeature(Window.FEATURE\u ACTION\u BAR);super.onCreate(savedInstanceState);setContentView(R.layou.activity\u edit);//初始化viewPager=(viewPager)findViewById(R.id.pager);Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar);//setSupportActionBar(Toolbar);actionBar=(actionBar)getSupportActionBar(Toolbar);但表示“无法解析getSupportActionBar”“所说的
Toolbar
是指
android.support.v7.widget.Toolbar
还是
android.widget.Toolbar
?您必须先使用。嗨,Mixel,我使用了android.support.v7.widget.Toolbar;您应该调用
getSupportActionBar()
,而不带我的答案中的参数。@Mixel:我使用了`viewPager=(viewPager)findViewById(R.id.pager);Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar);setSupportActionBar(Toolbar);actionBar=getActionBar();`我犯了错误。因为我在actionBar中仍然为null
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_edit);

    Toolbar toolbar = (Toolbar) findViewById(toolbar);
    setSupportActionBar(toolbar);

    actionBar = (ActionBar) getSupportActionBar();
}