Android : 包含工具栏后应用程序崩溃

Android : App Crash After Included Toolbar

第一次在 Android

中创建工具栏

styles.xml

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>

它向我显示错误 android.widget.LinearLayout cannot be cast to androidx.appcompat.widget.Toolbar 在我将其更改为 NoActionBar 之后,即使我将其更改为 RelativeLayout 然后也是相同的错误

首先,我创建了一个新的 Xml 工具栏布局,其中 RootElement 作为 Xml 布局中的工具栏,我无法创建选项因为它显示错误

" View requires API level 21 (current min is 16): <Toolbar> " 为此,我尝试添加 android.support.v7.widget... 它也显示错误,我尝试迁移到 AndroidX

之后我用新的 Xml File as RootElement as RelativeLayout

创建了我的工具栏
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?attr/actionBarSize"
        app:titleTextColor="@android:color/white"
        android:background="?attr/colorPrimary"
        />
</RelativeLayout>

然后我将该工具栏包含在 activity_main.xml

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

然后我声明在Activity.java

package com.Karth.check;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import androidx.appcompat.widget.Toolbar;

public class MainActivity extends AppCompatActivity {

    Toolbar mToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

       mToolbar=findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
    }
}

我 运行 我的应用程序要检查它是否已添加..但是我的应用程序崩溃并显示错误

2020-08-04 17:50:17.973 18038-18038/com.Karth.check E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.Karth.checkslate, PID: 18038
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Karth.check/com.Karth.check.MainActivity}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
        at androidx.appcompat.app.AppCompatDelegateImpl.setSupportActionBar(AppCompatDelegateImpl.java:421)
        at androidx.appcompat.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:150)
        at com.Karth.checkslate.MainActivity.onCreate(MainActivity.java:18)
        at android.app.Activity.performCreate(Activity.java:7136)
        at android.app.Activity.performCreate(Activity.java:7127)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6669) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

Manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.Karth.check">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.1"

    defaultConfig {
        applicationId "com.Karth.check"
        minSdkVersion 16
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.0.0'
    implementation 'com.google.android.material:material:1.1.0'
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'


}
 <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar"> 

在您的样式名称中包含请求 ActionBar 的“DarkActionBar”。所以你有 2 个解决方案,使用另一个没有 ActionBar 的主题,如“Theme.MaterialComponents.Light.NoActionBar”,或者覆盖它以不请求 actionBar

有 2 个问题:

This Activity already has an action bar supplied by the window decor

将您的应用主题更改为:

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

android.widget.LinearLayout cannot be cast to androidx.appcompat.widget.Toolbar

将布局中的 include 标记更改为:

<include layout="@layout/toolbar" />

第 1 步: 转到您的 添加

<item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>

第 2 步:Change <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar"> 进入 <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar" >

第 3 步:转到您的 activity.xml 并删除您的布局 ID