设置工具栏时出现NullPointerException

时间:2015-03-19 22:54:18

标签: java android android-toolbar

我有以下XML布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/mycloud">

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


</LinearLayout>

包含tool_bar:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/primaryColour"
    android:paddingTop="0dp"
    app:title="Noteu"
    app:theme="@style/myCustomToolBarTheme"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
    >

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

但是,当我尝试使用以下类设置工具栏时:

import android.nfc.Tag;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;

import it.neokree.materialtabs.MaterialTab;
import it.neokree.materialtabs.MaterialTabHost;
import it.neokree.materialtabs.MaterialTabListener;

public class MyCloud extends ActionBarActivity implements MaterialTabListener{

    private Toolbar toolbar;

    private MaterialTabHost tabHost;
    private ViewPager viewPager;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.mycloud);

        toolbar = (Toolbar) findViewById(R.id.tool_bar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    }

}

我通过Logcat收到以下错误:

3-19 22:47:33.183  29878-29878/uk.co.noteu E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: uk.co.noteu, PID: 29878
    java.lang.RuntimeException: Unable to start activity ComponentInfo{uk.co.noteu/uk.co.noteu.MyCloud}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2314)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
            at android.app.ActivityThread.access$800(ActivityThread.java:148)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5312)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
            at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:95)
            at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:88)
            at android.support.v7.internal.app.ToolbarActionBar.<init>(ToolbarActionBar.java:82)
            at android.support.v7.app.ActionBarActivityDelegateBase.setSupportActionBar(ActionBarActivityDelegateBase.java:189)
            at android.support.v7.app.ActionBarActivity.setSupportActionBar(ActionBarActivity.java:92)
            at uk.co.noteu.MyCloud.onCreate(MyCloud.java:35)
            at android.app.Activity.performCreate(Activity.java:5953)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1128)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
            at android.app.ActivityThread.access$800(ActivityThread.java:148)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5312)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372

工具栏是较大应用程序的一部分,但我已将问题缩小到该范围,并删除了上述所有其他代码。代码的编译和测试就像上面一样。

3 个答案:

答案 0 :(得分:8)

布局和代码中工具栏的ID不匹配,您使用的是布局名称而不是实际ID。 您可能在findViewById期间获取空指针,并将null设置为操作栏。

答案 1 :(得分:1)

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

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

在LayoutFile中查找包含的视图。轻松访问工具栏,无需任何错误。

答案 2 :(得分:0)

工具栏ID应匹配。

为版本21及更高版本创建文件夹(values-v21)并将以下代码粘贴到style.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

        <!-- android theme colors -->
        <item name="android:colorPrimary">@color/theme_dialer_primary</item>
        <item name="android:colorPrimaryDark">@color/theme_dialer_high</item>
        <item name="android:colorAccent">@color/theme_dialer_accent</item>
        <item name="android:navigationBarColor">@color/theme_dialer_primary</item>

        <!-- android transitions -->
        <item name="android:windowContentTransitions">true</item>
        <item name="android:windowAllowEnterTransitionOverlap">true</item>
        <item name="android:windowAllowReturnTransitionOverlap">true</item>
        <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
    </style>


    <style name="TransitionSampleActivity" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:colorPrimaryDark">@color/color_set_1_primary</item>
        <item name="android:colorPrimary">@color/color_set_1_primary</item>
        <item name="android:colorAccent">@color/color_set_1_accent</item>
    </style>

    <style name="TransitionSampleSecondActivity" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:colorPrimaryDark">@color/color_set_1_primary</item>
        <item name="android:colorPrimary">@color/color_set_1_primary</item>
        <item name="android:colorAccent">@color/color_set_1_accent</item>
    </style>

    <style name="theme1" parent="AppTheme">
        <item name="android:colorPrimary">@color/color_set_1_primary</item>
        <item name="android:colorControlActivated">@color/color_set_1_accent</item>
        <item name="android:colorAccent">@color/color_set_1_accent</item>
    </style>

    <style name="theme2" parent="AppTheme">
        <item name="android:colorPrimary">@color/color_set_2_primary</item>
        <item name="android:colorControlActivated">@color/color_set_2_accent</item>
        <item name="android:colorAccent">@color/color_set_2_accent</item>
    </style>

    <style name="theme3" parent="AppTheme">
        <item name="android:colorPrimary">@color/color_set_3_primary</item>
        <item name="android:colorControlActivated">@color/color_set_3_accent</item>
        <item name="android:colorAccent">@color/color_set_3_accent</item>
    </style>

    <style name="theme4" parent="AppTheme">
        <item name="android:colorPrimary">@color/color_set_4_primary</item>
        <item name="android:colorControlActivated">@color/color_set_4_accent</item>
        <item name="android:colorAccent">@color/color_set_4_accent</item>
    </style>

    <style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
        <item name="spinBars">true</item>
        <item name="color">@android:color/white</item>
    </style>

    <style name="SlidingTextViewStyle" parent="@android:style/Widget.TextView">
        <item name="android:fontFamily">sans-serif-medium</item>
        <item name="android:textColor">#FFF</item>
    </style>
</resources>

在值文件夹style.xml下添加以下代码

<resources>
    <style name="AppTheme" parent="MyTheme.Base">
    </style>
    <!-- Base theme applied no matter what API -->
    <style name="MyTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
        <!--If you are using revision 22.1 please use just windowNoTitle. Without android:-->
        <item name="android:windowNoTitle">true</item>
        <!--We will be using the toolbar so no need to show ActionBar-->
        <item name="windowActionBar">false</item>
        <!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette-->
        <!-- colorPrimary is used for the default action bar background -->
        <item name="colorPrimary">#2196F3</item>
        <!-- colorPrimaryDark is used for the status bar -->
        <item name="colorPrimaryDark">#1976D2</item>
        <!-- colorAccent is used as the default value for colorControlActivated
             which is used to tint widgets -->
        <item name="colorAccent">#FF4081</item>
        <!-- You can also set colorControlNormal, colorControlActivated
            colorControlHighlight and colorSwitchThumbNormal. -->
    </style>
</resources>

在清单文件中:

 <application
        android:theme="@style/AppTheme" >

将工具栏更改为:

<android.support.v7.widget.Toolbar
            xmlns:local="http://schemas.android.com/apk/res-auto"
            android:id="@+id/toolbar"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:minHeight="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            local:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

工具栏ID应匹配。

现在可以在API版本21及更高版本以及更低版本中查看操作栏。