我正在尝试构建我的应用程序但没有成功..我尝试了几种方法但没有任何效果。例外是:
Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
我的 style.xml 是:
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- theme customizations -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:textColorPrimary">@color/ldrawer_color</item>
<item name="actionMenuTextColor">@color/ldrawer_color</item>
<item name="android:textColorSecondary">@color/ldrawer_color</item>
</style>
</resources>
正如你所看到的,我已经宣布
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
这是我的 manifest.xml
<application
android:allowBackup="true"
tools:replace="android:icon"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
还有我BaseActivity
用来扩展其他活动的
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
public abstract class BaseActivity extends AppCompatActivity {
public Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutResource());
toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(BaseActivity.this.getResources().getString(R.string.app_name));
//getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
protected abstract int getLayoutResource();
protected void setActionBarIcon(int iconRes) {
toolbar.setNavigationIcon(iconRes);
}
}
我不知道它为什么会崩溃..启动应用程序而不会崩溃的唯一方法是在 style.xml 中设置父级,Theme.AppCompat.Light.NoActionBar
但是这样状态栏是透明的并且没有颜色...