首页 \ 问答 \ 尝试导航到活动时主题操作栏错误(Theme Action Bar Error When Trying To Navigate To Activity)

尝试导航到活动时主题操作栏错误(Theme Action Bar Error When Trying To Navigate To Activity)

您好我有以下activity_main.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:id="@+id/coordinatorLayout"
    tools:context="com.xxxxxx.eventmanager.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/eventListView"
        android:layout_centerHorizontal="true" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

使用以下ActivityMain.java类:

package com.xxxxxx.eventmanager;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;

import com.xxxxxx.eventmanager.models.UpdaterModel;
import com.xxxxxx.eventmanager.services.UpdaterService;

public class MainActivity extends AppCompatActivity {

    // Shared Preferences
    private SharedPreferences settings;
    public static Context contextOfApplication;
    private UpdaterModel mUpdater;

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        settings = getSharedPreferences("App", MODE_PRIVATE);

        Intent ishintent = new Intent(this, UpdaterService.class);
        PendingIntent pintent = PendingIntent.getService(this, 0, ishintent, 0);
        AlarmManager alarm = (AlarmManager)getSystemService(this.ALARM_SERVICE);
        alarm.cancel(pintent);
        alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 10000, pintent);

        contextOfApplication = getApplicationContext();

        mUpdater = new UpdaterModel(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

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

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        switch (id) {
            case R.id.action_settings:
                Toast.makeText(MainActivity.this, "You selected the settings menu!", Toast.LENGTH_SHORT).show();
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void logEventsTask (View view) {
        Log.i("events", mUpdater.getEventData().toString());
    }

    public static Context getContextOfApplication(){
        return contextOfApplication;
    }

}

现在,当我尝试从我的LoginActivity导航到此活动时,如下所示:

Intent i = new Intent(LoginActivity.this, MainActivity.class);
LoginActivity.this.startActivity(i);

一起出现这个错误:

FATAL EXCEPTION: main
     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxxxxx.eventmanager/com.xxxxxx.eventmanager.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:2059)
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
         at android.app.ActivityThread.access$600(ActivityThread.java:130)
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
         at android.os.Handler.dispatchMessage(Handler.java:99)
         at android.os.Looper.loop(Looper.java:137)
         at android.app.ActivityThread.main(ActivityThread.java:4745)
         at java.lang.reflect.Method.invokeNative(Native Method)
         at java.lang.reflect.Method.invoke(Method.java:511)
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
         at dalvik.system.NativeStart.main(Native Method)
      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 android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:198)
         at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:99)
         at com.xxxxxx.eventmanager.MainActivity.onCreate(MainActivity.java:34)
         at android.app.Activity.performCreate(Activity.java:5008)
         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
         at android.app.ActivityThread.access$600(ActivityThread.java:130) 
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
         at android.os.Handler.dispatchMessage(Handler.java:99) 
         at android.os.Looper.loop(Looper.java:137) 
         at android.app.ActivityThread.main(ActivityThread.java:4745) 
         at java.lang.reflect.Method.invokeNative(Native Method) 
         at java.lang.reflect.Method.invoke(Method.java:511)

我是Java和Android编程的新手,所以我遇到了麻烦,目前还不知道如何解决这个问题。

这是我的styles.xml文件:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Base.Theme.AppCompat.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>
    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>

Hello I have the following activity_main.xml file:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:id="@+id/coordinatorLayout"
    tools:context="com.xxxxxx.eventmanager.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/eventListView"
        android:layout_centerHorizontal="true" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

With the following ActivityMain.java class:

package com.xxxxxx.eventmanager;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;

import com.xxxxxx.eventmanager.models.UpdaterModel;
import com.xxxxxx.eventmanager.services.UpdaterService;

public class MainActivity extends AppCompatActivity {

    // Shared Preferences
    private SharedPreferences settings;
    public static Context contextOfApplication;
    private UpdaterModel mUpdater;

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        settings = getSharedPreferences("App", MODE_PRIVATE);

        Intent ishintent = new Intent(this, UpdaterService.class);
        PendingIntent pintent = PendingIntent.getService(this, 0, ishintent, 0);
        AlarmManager alarm = (AlarmManager)getSystemService(this.ALARM_SERVICE);
        alarm.cancel(pintent);
        alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 10000, pintent);

        contextOfApplication = getApplicationContext();

        mUpdater = new UpdaterModel(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

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

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        switch (id) {
            case R.id.action_settings:
                Toast.makeText(MainActivity.this, "You selected the settings menu!", Toast.LENGTH_SHORT).show();
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void logEventsTask (View view) {
        Log.i("events", mUpdater.getEventData().toString());
    }

    public static Context getContextOfApplication(){
        return contextOfApplication;
    }

}

Now when I try to navigate to this activity from my LoginActivity like this:

Intent i = new Intent(LoginActivity.this, MainActivity.class);
LoginActivity.this.startActivity(i);

Along comes this error:

FATAL EXCEPTION: main
     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxxxxx.eventmanager/com.xxxxxx.eventmanager.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:2059)
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
         at android.app.ActivityThread.access$600(ActivityThread.java:130)
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
         at android.os.Handler.dispatchMessage(Handler.java:99)
         at android.os.Looper.loop(Looper.java:137)
         at android.app.ActivityThread.main(ActivityThread.java:4745)
         at java.lang.reflect.Method.invokeNative(Native Method)
         at java.lang.reflect.Method.invoke(Method.java:511)
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
         at dalvik.system.NativeStart.main(Native Method)
      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 android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:198)
         at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:99)
         at com.xxxxxx.eventmanager.MainActivity.onCreate(MainActivity.java:34)
         at android.app.Activity.performCreate(Activity.java:5008)
         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
         at android.app.ActivityThread.access$600(ActivityThread.java:130) 
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
         at android.os.Handler.dispatchMessage(Handler.java:99) 
         at android.os.Looper.loop(Looper.java:137) 
         at android.app.ActivityThread.main(ActivityThread.java:4745) 
         at java.lang.reflect.Method.invokeNative(Native Method) 
         at java.lang.reflect.Method.invoke(Method.java:511)

I am new to Java and Android programming so I'm having trouble with this and do not currently have an idea of how to solve this issue.

This is my styles.xml file:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Base.Theme.AppCompat.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>
    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>

原文:https://stackoverflow.com/questions/36024244
更新时间:2019-12-03 09:45

最满意答案

错误说:

java.lang.IllegalStateException:此Activity已经有一个由窗口装饰提供的操作栏。

因此,由于您使用appcompat工具栏,在您的android清单活动标记中,您应该像这样指定它:

<manifest>
    ...
    <application>
        ....
        <activity 
            android:name="com.xxxxxx.eventmanager.MainActivity"
            android:theme="@style/AppTheme.NoActionBar"/>

    </application>
</manifest>

The error says:

java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor.

Thus, since you use the appcompat toolbar, in your android manifest activity tag, you should specify it like this:

<manifest>
    ...
    <application>
        ....
        <activity 
            android:name="com.xxxxxx.eventmanager.MainActivity"
            android:theme="@style/AppTheme.NoActionBar"/>

    </application>
</manifest>
2016-03-15

相关问答

更多

struts 2 jsp单选按钮动作(struts 2 jsp radio button action)

使用AjaxWebparts库中的Ajax更新字段标记这是链接和示例 http://ajaxtags.sourceforge.net/usage.html 希望这可以帮助 :) use Ajax update field tag from the AjaxWebparts library Here is the link and the example http://ajaxtags.sourceforge.net/usage.html hope this helps :)

无法使用角度ng-repeat和jquery在HTML表格中显示行(Cant display row in an HTML table using angular ng-repeat and jquery)

首先,我建议你阅读这个主题: “在AngularJS中思考”,如果我有一个jQuery背景? 显然你是以错误的方式使用jquery + angular。 并且您的jQuery代码中存在逻辑错误。 如果你想以角度方式解决问题: 一种可能的解决方案是使用范围变量或对象变量和ng-show指令。 这样的事情: https : //jsfiddle.net/p7ew017u/4/ <td ng-class="expand" ng-click="d.expanded = !d.expanded">+</t

除了/ api / v1以外的api请求返回403“禁止”(api request other than /api/v1 return 403 “Forbidden”)

虽然我没有完成该教程,但您提出请求的服务帐户无法访问群集级信息,例如列出命名空间。 RBAC(基于角色的访问控制)将用户与Role或ClusterRole绑定在一起,授予他们不同的权限。 我的猜测是服务帐户不应该知道存在哪些其他命名空间,因此无权列出它们。 在“修复”这个方面,除了创建具有正确权限的serviceaccount / user之外,该教程还对$HOME/.kube/config存储的配置文件进行了多次引用,该文件存储了应该具有访问权限的用户的凭据。集群级资源,包括列出名称空间。 你

在另一个文件中使用Asyncio循环引用(Using Asyncio loop reference in another file)

这行绝对不正确: mktoTask.add_done_callback( functools.partial( initSalesforce, "myparam1") 您不能向add_done_callback发送异步函数,这需要常规函数。 调用时,async / coroutine函数返回一个协程对象,而不执行任何代码。 要实际执行代码,必须将协程对象提交给事件循环。 在您的情况下,它将被删除,而无需执行函数内的代码。 (这很像调用生成器,但永远不会将返回的迭代器传递给将从中提取值的东西。)

为什么在此示例中可以返回临时对象?(Why the temporary object can be returned in this example?)

关键是对象的生命时间 。 对于第一种情况,正如您所说,创建了一个临时的std::string并将其传递给version2 (绑定到参数s2 )。 在完全表达之后,临时将被销毁,即整个result = version2(input, "###"); 。 这意味着版本2返回的引用仍然有效,可以使用它来分配result 。 所有临时对象都被销毁,作为评估(词法上)包含创建它们的点的完整表达式的最后一步, 对于第二种情况,在version3创建本地对象temp ,它将在退出version3时被销毁。 这

当通过API向松弛发送消息时,Curl和PHP无法正确传递新行(Curl and PHP not passing new line correctly when sending message to slack via API)

更改: "text" => 'First Line of Text\nSecond Line of Text', 至: "text" => "First Line of Text\nSecond Line of Text", 在PHP中使用单引号将字面输出\ n为\ n,而不是换行符。 Change: "text" => 'First Line of Text\nSecond Line of Text', To: "text" => "First Line of Text\nSecond

将信息从多个复选框发送到阵列Knockout js(send information from multiple checkbox to array Knockout js)

得了! 你试图绕过错误的一个。 self.invoicedatasintable observable拥有您的所有数据 视图模型: self.arr = ko.observableArray(); self.getCheckedInvoices = function() { ko.utils.arrayForEach(self.invoicedatasintable(),function(item){ //use ko.utils foreach if (item.checkedI

如何查看当前用户的密码,c#(How do I see the password of the current user, c#)

您无法以这种方式访问用户的密码。 密码不可逆,并且是经过哈希处理的。 这是一种单向操作。 如果要使用用户的现有凭据,可以使用: System.Net.CredentialCache.DefaultNetworkCredentials //for network 和 System.Net.CredentialCache.DefaultCredentials //for local You are unable to access a user's password in this way. P

相关文章

更多

最新问答

更多
  • 根据Woocommerce中的自定义字段计算自定义购物车商品价格(Custom cart item price calculation based on dimentions custom fields in Woocommerce)
  • 分开foreach项目并单独打印(Separate foreach items and print them individually)
  • 写模式管道是否同步?(Is write mode pipe synchronous?)
  • 发现可用的Windows Phone强调色和本地化名称(Discover available Windows Phone accent colors and localized names)
  • 使用多线程和WPF更新集合(Update Collection with multithreading and WPF)
  • 如何在字符串的情况下使用同步?(How should the synchronization be used in case of strings?)
  • 打印唯一的行,比较不超过N个字符(Print unique lines, compare no more than N characters)
  • Javascript / jQuery - 如何调用switch case从另一个函数执行(Javascript/jQuery - how to call a switch case to execute from another function)
  • Hbase超时错误不断发生(Hbase timeout errors keep occuring)
  • 如何在Delphi中更改TabControl中活动TAB的颜色(How to change the color of active TAB in a TabControl, in Delphi)
  • 正则表达式:在sublime文本中替换一些PHP代码(Regex: replace some pieces of php code in sublime text)
  • 在带有Scene2D的LibGDX中,如何在按下按钮时连续向右走?(In LibGDX with Scene2D, how can I continuously walk to the right when a button is pressed?)
  • 累加器如何在Haskell中工作?(How do accumulators work in Haskell?)
  • 使用开关检查市场和前缀与正确的货币符号(using switch to check market and prefix with correct currency symbol)
  • 在哪里分配一次使用类?(Where to allocate one time use class?)
  • 如何从两个DateTime / NaiveDateTime获取持续时间?(How do I get Duration from two DateTime / NaiveDateTime?)
  • 解析TimeSpan大于24小时?(Parse a TimeSpan greater than 24 hours? [duplicate])
  • 如何在球拍中本地更改阅读规则?(How to locally change reading rules in racket?)
  • 数据库应该由DI注入时的模拟存储(通过构造函数)(Mock storage when database should be injected by DI (through constructor))
  • 使用maven集成2个eclipse项目(Integrate 2 eclipse projects using maven)
  • 角度ng-repeat不检测变化(Angular ng-repeat not detecting changes)
  • Xaml组件在Silverlight + XNA应用程序的多个页面中可见(Xaml component visible in multiple pages in Silverlight+XNA applications)
  • 如何将`var`变量等同于另一个查询(How to equate `var` variable to another query)
  • 如何设计hyperledger链代码以适合您的业务?(how to design your chaincode of hyperledger to fit for your business?)
  • PHP readfile错误(PHP readfile error)
  • 在Date之后排序列表然后是时间(Sorting list after Date then time)
  • Android内部版本号(Android build number)
  • 在没有预设退出条件的情况下停止无限循环(stopping an infinite loop with no preset exit condition)
  • Phonegap应用程序全屏通过html页面中的按钮(Phonegap Application fullscreen through a button in html page)
  • PAA是否适合在门户网站中自动执行wcm库部署和设置?(Is PAA a good candidate for automating wcm library deployment and setup in portal?)