我目前正在学习如何在android中创建ToolBar
。我编写的代码在构建时没有给我任何错误,但在运行时压缩。当错误出现在行setContentView(R.layout.activity_main)
中时,我认为错误是在XML中的某个地方。你能告诉我我做错了什么吗?
MainActivity.java
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.app_bar);
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) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
if(id == R.id.navigate){
//startActivity(new Intent(this, SubActivity.class));
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainMenu">
<include
android:id="@+id/app_bar"
layout="@layout/app_bar"/>
<TextView
android:text="Hello World!"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
app_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:iosched="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
iosched:theme="@style/ThemeOverlay.AppCompat.Light"
iosched:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
>
</android.support.v7.widget.Toolbar>
styles.xml
<resources>
<style name="AppTheme." parent="AppTheme.Base">
</style>
<style name="AppTheme.Base" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
EDIT 堆栈跟踪:
12-20 16:00:50.238 14666-14666/com.example.joanna.fragmentn E/AndroidRuntime: FATAL EXCEPTION: main
12-20 16:00:50.238 14666-14666/com.example.joanna.fragmentn E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.joanna.fragmentn/com.example.joanna.fragmentn.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
12-20 16:00:50.238 14666-14666/com.example.joanna.fragmentn E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2081)
12-20 16:00:50.238 14666-14666/com.example.joanna.fragmentn E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2106)
12-20 16:00:50.238 14666-14666/com.example.joanna.fragmentn E/AndroidRuntime: at android.app.ActivityThread.access$700(ActivityThread.java:134)
12-20 16:00:50.238 14666-14666/com.example.joanna.fragmentn E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1217)
12-20 16:00:50.238 14666-14666/com.example.joanna.fragmentn E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
12-20 16:00:50.238 14666-14666/com.example.joanna.fragmentn E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
12-20 16:00:50.238 14666-14666/com.example.joanna.fragmentn E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:4856)
12-20 16:00:50.238 14666-14666/com.example.joanna.fragmentn E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
12-20 16:00:50.238 14666-14666/com.example.joanna.fragmentn E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
12-20 16:00:50.238 14666-14666/com.example.joanna.fragmentn E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
12-20 16:00:50.238 14666-14666/com.example.joanna.fragmentn E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
12-20 16:00:50.238 14666-14666/com.example.joanna.fragmentn E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
12-20 16:00:50.238 14666-14666/com.example.joanna.fragmentn E/AndroidRuntime: Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
12-20 16:00:50.238 14666-14666/com.example.joanna.fragmentn E/AndroidRuntime: at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:310)
12-20 16:00:50.238 14666-14666/com.example.joanna.fragmentn E/AndroidRuntime: at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:279)
12-20 16:00:50.238 14666-14666/com.example.joanna.fragmentn E/AndroidRuntime: at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:253)
12-20 16:00:50.238 14666-14666/com.example.joanna.fragmentn E/AndroidRuntime: at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
12-20 16:00:50.238 14666-14666/com.example.joanna.fragmentn E/AndroidRuntime: at com.example.joanna.fragmentn.MainActivity.onCreate(MainActivity.java:19)
12-20 16:00:50.238 14666-14666/com.example.joanna.fragmentn E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5047)
12-20 16:00:50.238 14666-14666/com.example.joanna.fragmentn E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
12-20 16:00:50.238 14666-14666/com.example.joanna.fragmentn E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2045)
12-20 16:00:50.238 14666-14666/com.example.joanna.fragmentn E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2106)
12-20 16:00:50.238 14666-14666/com.example.joanna.fragmentn E/AndroidRuntime: at android.app.ActivityThread.access$700(ActivityThread.java:134)
12-20 16:00:50.238 14666-14666/com.example.joanna.fragmentn E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1217)
12-20 16:00:50.238 14666-14666/com.example.joanna.fragmentn E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
12-20 16:00:50.238 14666-14666/com.example.joanna.fragmentn E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
12-20 16:00:50.238 14666-14666/com.example.joanna.fragmentn E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:4856)
12-20 16:00:50.238 14666-14666/com.example.joanna.fragmentn E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
12-20 16:00:50.238 14666-14666/com.example.joanna.fragmentn E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
12-20 16:00:50.238 14666-14666/com.example.joanna.fragmentn E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
12-20 16:00:50.238 14666-14666/com.example.joanna.fragmentn E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
12-20 16:00:50.238 14666-14666/com.example.joanna.fragmentn E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)