崩溃时应用工具栏 [英] Crashed when applied Toolbar

查看:135
本文介绍了崩溃时应用工具栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的Andr​​oid的编程。我有我申请的工具栏,我的应用程序,它崩溃,当我尝试运行应用程序这个问题。这里是code:

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:

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>

和这里是我的 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.access$600(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

谁能告诉我是什么问题?我已经包含在我的项目appcompat_v7在一起。谢谢你。

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

推荐答案

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

You can see in your logcat output the answer:

不要求Window.FEATURE_ACTION_BAR,   设置windowActionBar为false,你的主题使用工具栏代替。

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

也可以尝试使用<一个href="http://stackoverflow.com/questions/26515058/this-activity-already-has-an-action-bar-supplied-by-the-window-decor">this链接这<一href="http://stackoverflow.com/questions/26491689/how-do-i-declare-an-extended-height-toolbar-action-bar-on-android-lollipop">link

请确保您使用此:

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

在你的主题

这篇关于崩溃时应用工具栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆