java - Android 启动时崩溃 : Styling the ActionBar Theme App. 兼容

标签 java android android-appcompat android-toolbar

我不想发帖,因为它看起来很简单。为了简单起见,我将提供尽可能多的细节,而不会抛出一堆 logcat 并期望解决所有问题。

按照谷歌教程设置操作栏样式。 Win7、Android Studio、Android 5、API 19 KitKat(Min SDK Version 11)没有支持库,我认为 Gradle 1.8。

MainActivity.java 摘录:

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //ERRORS: // super.onCreate(savedInstanceState);
        //ERRORS: // setContentView(R.layout.activity_main);
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }

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

    @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.
        switch (item.getItemId()) {
            case R.id.action_search:
                // openSearch();
                return true;
            case R.id.action_settings:
                // openSettings();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

LogCat 摘录(当 SuperNotCalled 时):

Process: gaga.june, PID: 8726
    android.util.SuperNotCalledException: Activity {gaga.june/gaga.june.MainActivity} did not call through to super.onCreate()

LogCat 摘录(当我放入 Super 时):

Unable to start activity ComponentInfo{gaga.june/gaga.june.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

AndroidManifest.xml 摘录:

    <uses-sdk android:minSdkVersion="11"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/CustomActionBarTheme"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:parentActivityName="gaga.june.MainActivity" >
            <!-- Parent activity meta-data to support 4.0 and lower -->
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="gaga.june.MainActivity" />

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

我尝试过的:

  • 通常在 onCreate 中我会放入 super.onCreate(savedInstanceState); 但是,当我调试该行时,该行会引发错误,因此其他一些人将其注释掉,以免继承之前覆盖的 onCreate。我也这么做了

  • setContentView(R.layout.activity_main);调试时也给我一个错误。检查了手册,他们说如果它给你这样的错误,你必须定义ListView。但我没有使用 ListView ,我只定义了 TextView所以我把它注释掉了

  • 我将风格从 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 更改为至<style name="AppTheme" parent="Theme.AppCompat.Light">

  • 除此之外,我几乎完全按照教程来完成我的项目。如果我能确定所有这些问题意味着什么,那就太好了(所以总是最后的手段,我可​​能会跳过本教程)。非常感谢

    从主题.xml 编辑 CustomActionBarTheme:

    <!-- Theme applied to app/activity -->
    <style name="CustomActionBarTheme"
        parent="@android:style/Theme.Holo.Light">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
        <item name="actionBarStyle">@style/MyActionBar</item>
    </style>
    

    最佳答案

    首先在您的 onCreate 方法中:

    • 你必须调用 super 方法
    • 您必须使用 setContentView 方法定义布局
    • 您必须使用 getSupportActionBar() 而不是 getActionBar 方法

    类似于:

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

    然后您必须更改 MainActivity 使用的 CustomActionBarTheme 样式,因为 ActionBarActivity 需要 AppCompat主题

    <!-- Theme applied to app/activity -->
    <style name="CustomActionBarTheme"
        parent="Theme.AppCompat.Light">
           ......
    </style>
    

    最后,我建议您切换到新的应用程序兼容 v 22.2.0,更改您的 build.gradle

    dependencies {
         compile 'com.android.support:appcompat-v7:22.2.0'
    }
    

    在此版本中,ActionBarActivity 已弃用。您现在可以使用AppCompatActivity

    关于java - Android 启动时崩溃 : Styling the ActionBar Theme App. 兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30653161/

    相关文章:

    java - 使用 CMIS 创建两个文件 (Alfresco CE 4.2.c) 之间的关系

    java - 两种类型的通用类

    java - RequestParam.defaultValue 值

    java - (Android 应用程序)尝试将 AlertDialog .setIcon() 设置为从 URL 下载的图像

    android - 使用 Appium 在三星 Galaxy S6 上安装 Yahoo Mail apk 时出现 INSTALL_FAILED_DUPLICATE_PERMISSION 错误

    java - 删除android中的路径

    java - 我的游戏没有登录 Google Play 游戏

    Android:无法更改后退箭头导航图标的颜色

    android - ActionBar 项目总是出现在溢出菜单中

    android - 如何更改工具栏导航和溢出菜单图标(appcompat v7)?