我不想发帖,因为它看起来很简单。为了简单起见,我将提供尽可能多的细节,而不会抛出一堆 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/