【问题标题】:Android : App Crash After Included ToolbarAndroid:包含工具栏后应用程序崩溃
【发布时间】:2020-11-24 17:44:17
【问题描述】:

第一次在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>

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

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

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

之后,我使用新的 Xml 文件作为 RootElement 作为 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'


}

【问题讨论】:

  • 这个 Activity 已经有一个由窗口装饰提供的操作栏。不要在主题中请求 Window.FEATURE_SUPPORT_ACTION_BAR 并将 windowActionBar 设置为 false 以使用工具栏。
  • 它显示错误android.widget.LinearLayout cannot be cast to androidx.appcompat.widget.Toolbar 在我将其更改为** NoActionBar** 即使我将其更改为相对布局它也显示错误
  • 您是否在主题中将 windowActionBar 设置为 false?

标签: java android xml android-layout android-toolbar


【解决方案1】:

第 1 步: 转到您的 添加

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

第 2 步:Change &lt;style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar"&gt; 变为 &lt;style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar" &gt;

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

【讨论】:

    【解决方案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" />
    

    【讨论】:

    • 我想
    • @Karthickyuvan 只需使用没有 android:id="@+id/toolbar" 属性的答案中报告的 xml。
    • 谢谢 :) 你能说说为什么我们不在这里添加 ID 吗?当我添加 ID 时,我得到了错误
    • @Karthickyuvan 因为在包含标签中添加 id 包含布局的 根视图 具有此 id。它不是Toolbar,问题发生在这里:mToolbar=findViewById(R.id.toolbar);
    【解决方案3】:
     <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar"> 
    

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

    【讨论】:

    • 在我将其更改为 ** NoActionBar** 后,它显示错误 android.widget.LinearLayout cannot be cast to androidx.appcompat.widget.Toolbar **
    • 即使我将其更改为相对布局,它也会显示错误
    猜你喜欢
    • 2022-12-04
    • 1970-01-01
    • 2015-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多