如何将android工具栏添加到具有片段的活动中
我在Android Studio中创建了一个新项目——一个带有片段的空白活动。它创建了以下文件如何将android工具栏添加到具有片段的活动中,android,android-fragments,toolbar,Android,Android Fragments,Toolbar,我在Android Studio中创建了一个新项目——一个带有片段的空白活动。它创建了以下文件 mainActivity.java MainActivityFragment.java activity_main.xml 片段_main.xxml 现在我想添加一个工具栏,所以我创建了一个app_bar.xml,如下所示 <?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar xmlns
- mainActivity.java MainActivityFragment.java activity_main.xml 片段_main.xxml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
local:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include
android:id="@+id/app_bar"
layout="@layout/app_bar" />
<fragment
android:id="@+id/fragment"
android:name="com.lalapetstudios.udacityprojects.spotifystreamer.MainActivityFragment"
tools:layout="@layout/fragment_main" android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
activity_main.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment"
android:name="com.lalapetstudios.udacityprojects.spotifystreamer.MainActivityFragment"
tools:layout="@layout/fragment_main" android:layout_width="match_parent"
android:layout_height="match_parent" />
fragment.xml
<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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivityFragment">
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
当我运行这个。我得到以下例外
原因:java.lang.NullPointerException:尝试调用虚拟机
方法“java.lang.CharSequence”
空对象上的android.support.v7.widget.Toolbar.getTitle()
参考文献
因此,我修改了我的activity_main.xml,如下所示
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
local:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include
android:id="@+id/app_bar"
layout="@layout/app_bar" />
<fragment
android:id="@+id/fragment"
android:name="com.lalapetstudios.udacityprojects.spotifystreamer.MainActivityFragment"
tools:layout="@layout/fragment_main" android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
还修改了app_bar.xml,如下所示(删除了android:id参数)
现在应用程序运行了。我能看到工具栏。但是,该片段隐藏在工具栏后面。HelloWorld位于工具栏后面。我该怎么修呢。谢谢。将activity\u main.xml文件中的方向更改为垂直。见下文
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include
android:id="@+id/app_bar"
layout="@layout/app_bar" />
<fragment
android:id="@+id/fragment"
android:name="com.lalapetstudios.udacityprojects.spotifystreamer.MainActivityFragment"
tools:layout="@layout/fragment_main" android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
尝试在LinearLayout中设置android:orientation=“vertical”。您应该在activity\u main.xml的LinearLayout中设置android:orientation=“vertical”。这将满足您的需要。