使用 AppCompat 获取 NullPointerException

Getting a NullPointerException with AppCompat

我正在尝试使用 AppCompat v21 为 pre-5.0 实现 'Material design',但是当我尝试使用新的 'toolbar' 时出现空指针异常。我按照本指南来实现它 http://android-developers.blogspot.com.es/2014/10/appcompat-v21-material-design-for-pre.html。但是我找不到导致NPE的错误。

app.gradle

apply plugin: 'com.android.application'

android {
compileSdkVersion 21
buildToolsVersion "21.1.2"

defaultConfig {
    applicationId "com.example.alejandrohall.myapplication"
    minSdkVersion 14
    targetSdkVersion 21
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard- rules.pro'
    }
}
}

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

toolbar.xml

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_awesome_toolbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"

    android:background="?attr/colorPrimary"/>

styles.xml

<resources>

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



    <!-- Customize your theme here. -->

    <item name="colorPrimary">@color/material_blue_grey_950</item>
    <item name="windowActionModeOverlay">true</item>
</style>

</resources>

activity_mail.xml

<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=".MainActivity">


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="HOLA MUNDO"
    android:id="@+id/textView"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />


</RelativeLayout>

MainActivity.java 包裹 com.example.alejandrohall.myapplication;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;



public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

    }



}

错误

Process: com.example.alejandrohall.myapplication, PID: 6461
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.alejandrohall.myapplication/com.example.alejandrohall.myapplication.MainActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
        at android.app.ActivityThread.access0(ActivityThread.java:139)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5086)
        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:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.NullPointerException
        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:84)
        at android.support.v7.app.ActionBarActivityDelegateBase.setSupportActionBar(ActionBarActivityDelegateBase.java:175)
        at android.support.v7.app.ActionBarActivity.setSupportActionBar(ActionBarActivity.java:92)
        at com.example.alejandrohall.myapplication.MainActivity.onCreate(MainActivity.java:17)
        at android.app.Activity.performCreate(Activity.java:5248)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
                    at android.app.ActivityThread.access0(ActivityThread.java:139)
                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
                    at android.os.Handler.dispatchMessage(Handler.java:102)
                    at android.os.Looper.loop(Looper.java:136)
                    at android.app.ActivityThread.main(ActivityThread.java:5086)
                    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:785)
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                    at dalvik.system.NativeStart.main(Native Method)

您的 toolbar.xml 未包含在您的 activity_main.xml 中,因此 findViewById(R.id.my_awesome_toolbar) returns null - 您需要将 Toolbar 直接包含到布局中每个 activity.