NullPointerException异常,同时设置工具栏 [英] NullPointerException while setting toolbar

查看:319
本文介绍了NullPointerException异常,同时设置工具栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的XML布局:

I've got the following XML Layout :

<?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之中:

With the include tool_bar being :

<?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>

然而,当我尝试设置与下面的类工具栏:

However when I try to set the toolbar with the following class :

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中得到以下错误:

I get the following error via 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

工具栏是一个更大的应用程序的一部分,但我已经缩小问题到并删除了所有其他code。与什么上面留下。在code的编译和测试就像是上面。

The tool bar is part of a larger application but I've narrowed down the problem to that and removed all other code leaving with what's above. The code was compiled and tested like it is above.

推荐答案

工具栏的布局和code中的ID不匹配,您使用的布局,而不是实际ID的名称。
findViewById过程中你可能得到一个空指针和空设置为你的动作条。

The IDs of your toolbar in layout and code don't match, you used the name of the layout instead of the actual id. You're probably getting a null pointer during findViewById, and setting null as your actionbar.

这篇关于NullPointerException异常,同时设置工具栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆