Java Android-在Android中设置自定义工具栏时出现NullPointerException

Java Android-在Android中设置自定义工具栏时出现NullPointerException,java,android,xml,Java,Android,Xml,我正在尝试将自定义图像设置到标题栏中 运行以下代码将返回以下内容: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.me.project/com.me.project.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.Toolbar

我正在尝试将自定义图像设置到标题栏中

运行以下代码将返回以下内容:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.me.project/com.me.project.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.Toolbar.setTitle(int)' on a null object reference
工具栏
为空。为什么?
R.id.app\u工具栏
此处不为空

MainActivity.java:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Toolbar toolbar = findViewById(R.id.app_toolbar);
        toolbar.setTitle(R.string.app_name);
        toolbar.setLogo(R.drawable.app_icon);
        setSupportActionBar(toolbar);

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.container, new HomeScreenFragment());
        fragmentTransaction.commit();
    }
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_activity"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <include
        android:id="@+id/app_toolbar"
        layout="@layout/app_toolbar"/>

    <FrameLayout
        android:id="@+id/container"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>



</android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/app_toolbar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    >

</android.support.v7.widget.Toolbar>
活动\u main.xml:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Toolbar toolbar = findViewById(R.id.app_toolbar);
        toolbar.setTitle(R.string.app_name);
        toolbar.setLogo(R.drawable.app_icon);
        setSupportActionBar(toolbar);

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.container, new HomeScreenFragment());
        fragmentTransaction.commit();
    }
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_activity"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <include
        android:id="@+id/app_toolbar"
        layout="@layout/app_toolbar"/>

    <FrameLayout
        android:id="@+id/container"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>



</android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/app_toolbar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    >

</android.support.v7.widget.Toolbar>


您应该将
设置内容视图(R.layout.activity\u main)
添加到
onCreate()
您应该将
设置内容视图(R.layout.activity\u main)
添加到
onCreate()
首先将主题更改为NoActionBar,然后使用自定义工具栏

 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

首先将主题更改为NoActionBar,然后使用自定义工具栏

 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

在super.onCreate(savedInstanceState)之后添加此项


在super.onCreate(savedInstanceState)之后添加此项


这样做了,现在我得到
这个活动已经有了一个由窗口装饰提供的操作栏。不要请求Window.FEATURE\u SUPPORT\u ACTION\u BAR,并在主题中将windowActionBar设置为false以使用工具栏。
追溯到
设置支持ActionBar(工具栏)更新styles.xml。需要在活动中使用Methanks,这解决了错误。但是,现在自定义工具栏显示为背景。不管怎样,这是另一个问题。我这么做了,现在我得到了
这个活动已经有了一个由窗口装饰提供的操作栏。不要请求Window.FEATURE\u SUPPORT\u ACTION\u BAR,并在主题中将windowActionBar设置为false以使用工具栏。
追溯到
设置支持ActionBar(工具栏)更新styles.xml。需要在活动中使用Methanks,这解决了错误。但是,现在自定义工具栏显示为背景。这是另一个问题