其他分享
首页 > 其他分享> > android-使用AppCompat获取NullPointerException

android-使用AppCompat获取NullPointerException

作者:互联网

我正在尝试使用AppCompat v21在5.0之前的版本中实现“材料设计”,但是当我尝试使用新的“工具栏”时却遇到了空指针异常.我按照该指南实施了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.access$800(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.access$800(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)返回null-您需要将Toolbar直接包括在每个活动的布局中.

标签:android,android-layout,android-appcompat
来源: https://codeday.me/bug/20191012/1902978.html