[英]Android Crash on start: Styling the ActionBar Theme App.Compat
I don't want to post since it seems simple. 我不想发布,因为它看起来很简单。 For the sake of simplicity I'll provide as much detail as I can without throwing up a bunch of logcat and expecting a cure all.
为了简单起见,我将提供尽可能多的细节,而不会抛出很多麻烦,并且期待所有解决方案。
Following the google tutorial for styling an action bar. 按照google教程设置操作栏样式。 Win7, Android Studio, Android 5, API 19 KitKat (Min SDK Version 11) no support library, Gradle 1.8 I think.
Win7,Android Studio,Android 5,API 19 KitKat(最低SDK版本11)没有支持库,我认为Gradle 1.8。
MainActivity.java excerpt: 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 excerpt(when SuperNotCalled): LogCat摘录(当SuperNotCalled时):
Process: gaga.june, PID: 8726
android.util.SuperNotCalledException: Activity {gaga.june/gaga.june.MainActivity} did not call through to super.onCreate()
LogCat excerpt (when I put in the Super): 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 excerpt: 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>
WHAT I'VE TRIED: 我尝试过的内容:
Normally in the onCreate I would put super.onCreate(savedInstanceState);
通常,在onCreate中,我将放置
super.onCreate(savedInstanceState);
However, when I debug that line is the one that throws the error so some other people on SO commented it out to not inherit the previous overriden onCreate. 但是,当我调试该行时会引发错误,因此SO上的其他一些人将其注释为不继承先前重写的onCreate。 I did the same
我也一样
setContentView(R.layout.activity_main);
Also gives me an error when debugging. 调试时也给我一个错误。 Checked the manual, they said that you must define the ListView if it gives you an error like that.
检查了手册,他们说如果它给您这样的错误,则必须定义ListView。 But I'm not using a listview, I only defined
TextView
Therefore, I commented it out 但是我没有使用列表视图,我只定义了
TextView
因此,我将其注释掉
I changed my style from <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
to <style name="AppTheme" parent="Theme.AppCompat.Light">
我将样式从
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
更改为<style name="AppTheme" parent="Theme.AppCompat.Light">
EDIT CustomActionBarTheme from themes.xml: 从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>
First of all in your onCreate
method: 首先在您的
onCreate
方法中:
setContentView
method setContentView
方法定义布局 getSupportActionBar()
instead of getActionBar
method getSupportActionBar()
而不是getActionBar
方法 Something like: 就像是:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
Then you have to change the style CustomActionBarTheme
used by your MainActivity
, because the ActionBarActivity
requires an AppCompat
theme 然后,您必须更改
MainActivity
使用的CustomActionBarTheme
样式,因为ActionBarActivity
需要一个AppCompat
主题
<!-- Theme applied to app/activity -->
<style name="CustomActionBarTheme"
parent="Theme.AppCompat.Light">
......
</style>
Finally I suggest you switching to the new app-compat v 22.2.0 changing your build.gradle
最后,我建议您切换到新的app-compat v 22.2.0来更改
build.gradle
dependencies {
compile 'com.android.support:appcompat-v7:22.2.0'
}
With this version the ActionBarActivity
is deprecated. 在此版本中,不推荐使用
ActionBarActivity
。 You can use now the AppCompatActivity
您现在可以使用
AppCompatActivity
For Theme.AppCompat you need to include the support library in your gradle.build file: 对于Theme.AppCompat,您需要在gradle.build文件中包括支持库:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.0'
}
Additionally ActionBarActivity
is deprecated, use AppCompatActivity
此外,不建议使用
ActionBarActivity
,请使用AppCompatActivity
The reason you are having this problem is because the activity you are trying to apply the theme to is extending ActionBarActivity which requires the AppCompat theme to be applied. 您遇到此问题的原因是,您尝试将主题应用到的活动正在扩展ActionBarActivity,而这需要应用AppCompat主题。
Change the parent of the actual java code to be just plain Activity and you should be able to leave the theme on it. 将实际Java代码的父级更改为纯活动,您应该可以在其上保留主题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.