Java 应用工具栏时崩溃
我是Android编程新手。当我将工具栏应用到我的应用程序时,我遇到了这个问题,当我尝试运行应用程序时,它崩溃了。代码如下:Java 应用工具栏时崩溃,java,android,Java,Android,我是Android编程新手。当我将工具栏应用到我的应用程序时,我遇到了这个问题,当我尝试运行应用程序时,它崩溃了。代码如下: ToolbarActivity.java package com.example.toolbar; import android.support.v7.app.ActionBarActivity; import android.support.v7.widget.Toolbar; import android.os.Bundle; import android.vie
ToolbarActivity.java
package com.example.toolbar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class ToolbarActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_toolbar);
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.toolbar, menu);
return true;
}
}
activity_toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_awesome_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimaryDark"
app:theme="@style/ThemeOverlay.AppCompat.ActionBar">
</android.support.v7.widget.Toolbar>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="page 1"
/>
</LinearLayout>
有人能告诉我是什么问题吗?我将appcompat_v7与我的项目一起包括在内。谢谢。您可以在logcat输出中看到答案: 不要请求Window.FEATURE\u ACTION\u栏, 在主题中将windowActionBar设置为false,以使用工具栏 也可以尝试使用和这个 请确保使用以下选项:
<item name="windowActionBar">false</item>
false
在主题中如果使用v7支持中的
工具栏
,则应在活动中使用主题.appbatch
在styles.xml中定义您的样式
<style name="AppTheme" parent="Theme.AppCompat">
<item name="colorPrimary">@color/black</item>
</style>
<color name="black">#000000</color>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
尝试此修复,它对我有效:
另外,请检查以下使用AppCompat的链接:
我不太明白发生了什么事。之前,我创建了另一个具有类似代码的项目,它正在工作。知道如何更改工具栏中文本的颜色吗?@DanKCl在应用程序主题中使用textColorPrimary属性。在您和@SacreDeveloper的帮助下,它现在正在工作。。。非常感谢。请投赞成票,并在我的答案有帮助时接受它;)实际上,我无法让这个建议起作用。如果您阅读了,您会注意到必须使用NoActionBar
主题之一来防止应用程序将本机类用于操作栏。
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >