[英]Android Crash on start: Styling the ActionBar Theme App.Compat
我不想發布,因為它看起來很簡單。 為了簡單起見,我將提供盡可能多的細節,而不會拋出很多麻煩,並且期待所有解決方案。
按照google教程設置操作欄樣式。 Win7,Android Studio,Android 5,API 19 KitKat(最低SDK版本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。 但是我沒有使用列表視圖,我只定義了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>
首先在您的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
對於Theme.AppCompat,您需要在gradle.build文件中包括支持庫:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.0'
}
此外,不建議使用ActionBarActivity
,請使用AppCompatActivity
您遇到此問題的原因是,您嘗試將主題應用到的活動正在擴展ActionBarActivity,而這需要應用AppCompat主題。
將實際Java代碼的父級更改為純活動,您應該可以在其上保留主題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.