当前位置: 动力学知识库 > 问答 > 编程问答 >

android - How to create application toolbar?

问题描述:

I am currently learning how to create ToolBar in android. I have written the code that gives me no errors while building, but crushes on runtime. As the error appears in line setContentView(R.layout.activity_main) I assume that the mistake is somewhere in XMLs. Could You tell me what did I do wrong?

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

StackTrace:

 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)

网友答案:

Try to delete these lines:

iosched:theme="@style/ThemeOverlay.AppCompat.Light"
iosched:popupTheme="@style/ThemeOverlay.AppCompat.Dark"

If it wouldn't work change

xmlns:iosched="http://schemas.android.com/apk/res-auto"

to

xmlns:app="http://schemas.android.com/apk/res-auto"

Hope it help

分享给朋友:
您可能感兴趣的文章:
随机阅读: