java 应用工具栏时崩溃

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27087551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:10:51  来源:igfitidea点击:

Crashed when applied Toolbar

javaandroid

提问by DanKCl

I'm new in Android programming. I'm having this issue when I applied the toolbar into my app and it crashed when I try to run the app. Here is the code:

我是 Android 编程的新手。当我将工具栏应用到我的应用程序中时遇到了这个问题,当我尝试运行该应用程序时它崩溃了。这是代码:

ToolbarActivity.java

package com.example.toolbar;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class ToolbarActivity extends ActionBarActivity {

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
        if (toolbar != null) {
            setSupportActionBar(toolbar);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.toolbar, menu);
        return true;
    }

}

activity_toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <android.support.v7.widget.Toolbar
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/my_awesome_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimaryDark"

        app:theme="@style/ThemeOverlay.AppCompat.ActionBar">

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


    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="page 1"
        />

</LinearLayout>

and here's my LogCat:

这是我的LogCat

11-23 04:46:35.260: E/AndroidRuntime(1870): FATAL EXCEPTION: main 11-23 04:46:35.260: E/AndroidRuntime(1870): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.toolbar/com.example.toolbar.ToolbarActivity}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead. 
11-23 04:46:35.260: E/AndroidRuntime(1870): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211) 11-23 04:46:35.260: E/AndroidRuntime(1870): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 11-23 04:46:35.260: E/AndroidRuntime(1870): at android.app.ActivityThread.access0(ActivityThread.java:141) 11-23 04:46:35.260: E/AndroidRuntime(1870): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 11-23 04:46:35.260: E/AndroidRuntime(1870): at android.os.Handler.dispatchMessage(Handler.java:99)
11-23 04:46:35.260: E/AndroidRuntime(1870): at android.os.Looper.loop(Looper.java:137) 11-23 04:46:35.260: E/AndroidRuntime(1870): at android.app.ActivityThread.main(ActivityThread.java:5103) 11-23 04:46:35.260: E/AndroidRuntime(1870): at java.lang.reflect.Method.invokeNative(Native Method) 11-23 04:46:35.260: E/AndroidRuntime(1870): at java.lang.reflect.Method.invoke(Method.java:525) 11-23 04:46:35.260: E/AndroidRuntime(1870): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-23 04:46:35.260: E/AndroidRuntime(1870): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 11-23 04:46:35.260: E/AndroidRuntime(1870): at dalvik.system.NativeStart.main(Native Method) 11-23 04:46:35.260: E/AndroidRuntime(1870): Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
11-23 04:46:35.260: E/AndroidRuntime(1870): at android.support.v7.app.ActionBarActivityDelegateBase.setSupportActionBar(ActionB??arActivityDelegateBase.java:165) 11-23 04:46:35.260: E/AndroidRuntime(1870): at android.support.v7.app.ActionBarActivity.setSupportActionBar(ActionBarActivity.j??ava:92) 11-23 04:46:35.260: E/AndroidRuntime(1870): at com.example.toolbar.ToolbarActivity.onCreate(ToolbarActivity.java:19)
11-23 04:46:35.260: E/AndroidRuntime(1870): at android.app.Activity.performCreate(Activity.java:5133) 11-23 04:46:35.260: E/AndroidRuntime(1870): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 11-23 04:46:35.260: E/AndroidRuntime(1870): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175) 11-23 04:46:35.260: E/AndroidRuntime(1870): ... 11 more

Can someone tell me what is the problem? I had included the appcompat_v7 together with my project. Thanks.

有人可以告诉我有什么问题吗?我已经将 appcompat_v7 与我的项目一起包含在内。谢谢。

回答by SacreDeveloper

You can see in your logcat output the answer:

您可以在 logcat 输出中看到答案:

Do not request Window.FEATURE_ACTION_BAR, set windowActionBar to false in your theme to use a Toolbar instead.

不要请求 Window.FEATURE_ACTION_BAR,在您的主题中将 windowActionBar 设置为 false 以使用工具栏。

Also try using this linkand this link

也尝试使用此链接和此链接

make sure you use this:

确保你使用这个:

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

in your theme

在你的主题中

回答by klimat

If you use Toolbarfrom v7-support, you should use Theme.AppCombatin your Activity.

如果您使用Toolbarfrom v7-support,您应该Theme.AppCombat在您的Activity.

Define your style in styles.xml

定义你的风格 styles.xml

<style name="AppTheme" parent="Theme.AppCompat">
    <item name="colorPrimary">@color/black</item>
</style>
<color name="black">#000000</color>

And apply it to whole application (or specyfic Activity) in AndroidManifest.xml

并将其应用于整个应用程序(或特定ActivityAndroidManifest.xml

<application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

回答by V_J

Try this fix, it worked for me:

试试这个修复,它对我有用:

Link to the StackOverFlow answer

链接到 StackOverFlow 答案

Also check for the below link for using AppCompat:

还要检查以下链接以使用 AppCompat:

Reference link for the AppCompat library

AppCompat 库的参考链接