Android 启动时崩溃:设置 ActionBar 主题的样式 App.Compat
Android Crash on start: Styling the ActionBar Theme App.Compat
我不想 post 因为它看起来很简单。为了简单起见,我将提供尽可能多的细节,而不会抛出一堆 logcat 并期待治愈所有。
按照 google 教程来设置操作栏的样式。 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);
但是,当我调试时那一行是抛出错误的那一行所以其他一些人在 SO 上将其注释掉不继承之前覆盖的 onCreate。我也是这样
setContentView(R.layout.activity_main);
调试的时候也报错。检查了手册,他们说如果它给你这样的错误,你必须定义 ListView 。但是我没有使用listview,我只定义了TextView
因此,我把它注释掉了
我把风格从<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
改成<style name="AppTheme" parent="Theme.AppCompat.Light">
除此之外,我几乎完全按照教程来完成我的项目。如果我能确定所有这些问题意味着什么,那就太好了(所以总是不得已,我可能会跳过本教程)。非常感谢
从 themes.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>
对于 Theme.AppCompat,您需要在 gradle.build 文件中包含支持库:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.0'
}
此外 ActionBarActivity
已弃用,请使用 AppCompatActivity
首先在你的onCreate
方法中:
- 你必须调用超级方法
- 您必须使用
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>
最后我建议你切换到新的 app-compat v 22.2.0 改变你的 build.gradle
dependencies {
compile 'com.android.support:appcompat-v7:22.2.0'
}
在此版本中,ActionBarActivity
已弃用。您现在可以使用 AppCompatActivity
您遇到此问题的原因是 activity 您正在尝试
将主题应用到扩展 ActionBarActivity 其中
需要应用 AppCompat 主题。
将实际 java 代码的父级更改为普通 Activity 和
你应该可以留下主题。
我不想 post 因为它看起来很简单。为了简单起见,我将提供尽可能多的细节,而不会抛出一堆 logcat 并期待治愈所有。
按照 google 教程来设置操作栏的样式。 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);
但是,当我调试时那一行是抛出错误的那一行所以其他一些人在 SO 上将其注释掉不继承之前覆盖的 onCreate。我也是这样
setContentView(R.layout.activity_main);
调试的时候也报错。检查了手册,他们说如果它给你这样的错误,你必须定义 ListView 。但是我没有使用listview,我只定义了TextView
因此,我把它注释掉了
我把风格从<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
改成<style name="AppTheme" parent="Theme.AppCompat.Light">
从 themes.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>
对于 Theme.AppCompat,您需要在 gradle.build 文件中包含支持库:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.0'
}
此外 ActionBarActivity
已弃用,请使用 AppCompatActivity
首先在你的onCreate
方法中:
- 你必须调用超级方法
- 您必须使用
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>
最后我建议你切换到新的 app-compat v 22.2.0 改变你的 build.gradle
dependencies {
compile 'com.android.support:appcompat-v7:22.2.0'
}
在此版本中,ActionBarActivity
已弃用。您现在可以使用 AppCompatActivity
您遇到此问题的原因是 activity 您正在尝试 将主题应用到扩展 ActionBarActivity 其中 需要应用 AppCompat 主题。
将实际 java 代码的父级更改为普通 Activity 和 你应该可以留下主题。