无法将工具栏添加到Android项目

时间:2015-07-11 22:31:44

标签: java android xml

我试图添加一个自定义工具栏来将Action Bar替换为我的android项目。以下是我如何去做。

我在build.grade中添加了以下依赖项:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
}

我也设置了:

targetSdkVersion 21

我通过执行以下操作禁用了操作栏:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
    </style>

</resources>

我在布局目录中创建了一个名为app_bar.xml的新文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#EF6C00"
    android:elevation="4dp">

</android.support.v7.widget.Toolbar>

在我的main_activity.xml中,我把它包括在内:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <include
        android:id="@+id/app_bar"
        layout="@layout/app_bar"
        ></include>

</RelativeLayout>

在我的MainActivity.java中,我声明并初始化它

toolbar = (Toolbar)findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);

我在OnCreate外面声明的工具栏:

private Toolbar toolbar;

当我运行它时,我收到以下错误:

07-11 19:20:21.171    1332-1332/com.example.soufin.pullupsv3 E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.soufin.pullupsv3, PID: 1332
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.soufin.pullupsv3/com.example.soufin.pullupsv3.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:94)
            at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:87)
            at android.support.v7.internal.app.ToolbarActionBar.<init>(ToolbarActionBar.java:79)
            at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:198)
            at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:96)
            at com.example.soufin.pullupsv3.MainActivity.onCreate(MainActivity.java:61)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

我做错了什么?

固定IT:MainActivity中的其他元素未初始化。

0 个答案:

没有答案