"You need to use a Theme.AppCompat theme (or descendant) with this activity" 无法启动 activity
"You need to use a Theme.AppCompat theme (or descendant) with this activity" Unable to start activity
我在使用 android-support-v7-appcompat 并尝试向我的 activity 添加工具栏时遇到此错误。
values/styles.xml
<resources>
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base" />
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:windowFullscreen">true</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
</resources>
menu_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_refresh"
android:orderInCategory="100"
android:showAsAction="always"
android:title="Refresh"/>
<item
android:id="@+id/action_settings"
android:title="Settings">
</item>
</menu>
我的 activity XML 首先包括
<Android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:background="@android:color/black"
/>
我的 java activity 扩展了 AppCompatActivity 并具有这些代码行:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@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.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
在清单中
android:theme="@style/AppTheme"
有人知道吗?
像这样使用 Toolbar
:没有(A
只是小写 a
):
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:background="@android:color/black" />
然后,因为您已经拥有:
Theme.AppCompat.Light.NoActionBar
它应该工作 then.But,这将导致问题:
Theme.AppCompat.Light.DarkActionBar
此外,也添加这个(在 Styles.xml
中):
更新:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<!-- <item name="android:windowFullscreen">true</item> using for FullScreen Activities -->
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
</resources>
我刚刚发现问题所在。在清单中,我指定了 android:theme="@style/AppTheme"
在应用部分而不是 activity 部分!!
我在使用 android-support-v7-appcompat 并尝试向我的 activity 添加工具栏时遇到此错误。
values/styles.xml
<resources>
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base" />
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:windowFullscreen">true</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
</resources>
menu_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_refresh"
android:orderInCategory="100"
android:showAsAction="always"
android:title="Refresh"/>
<item
android:id="@+id/action_settings"
android:title="Settings">
</item>
</menu>
我的 activity XML 首先包括
<Android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:background="@android:color/black"
/>
我的 java activity 扩展了 AppCompatActivity 并具有这些代码行:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@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.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
在清单中
android:theme="@style/AppTheme"
有人知道吗?
像这样使用 Toolbar
:没有(A
只是小写 a
):
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:background="@android:color/black" />
然后,因为您已经拥有:
Theme.AppCompat.Light.NoActionBar
它应该工作 then.But,这将导致问题:
Theme.AppCompat.Light.DarkActionBar
此外,也添加这个(在 Styles.xml
中):
更新:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<!-- <item name="android:windowFullscreen">true</item> using for FullScreen Activities -->
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
</resources>
我刚刚发现问题所在。在清单中,我指定了 android:theme="@style/AppTheme"
在应用部分而不是 activity 部分!!