Activity 中的错误空对象引用
Error null object reference in Activity
执行此 activity 时得到 "Attempt to invoke virtual method 'boolean java.lang.String.equals (java.lang.Object)' on a null object reference",可能我的布局中缺少某些内容。请帮我解决这个问题,这是我目前的 layout.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical"
tools:context="com.sbi.mvicall.SettingPageActivity">
<include
android:id="@+id/toolbar"
layout="@layout/view_toolbar"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/language"
android:textSize="16sp"/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/language"
android:spinnerMode="dropdown"
android:layout_weight="1">
</Spinner>
</LinearLayout>
<view
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/grey_e4e4e4"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/about"
android:id="@+id/about"
android:textSize="16sp"/>
</LinearLayout>
这是我的 layout.java :
package com.sbi.mvicall;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class SettingPageActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting_page);
}
}
这是错误日志:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sbi.mvicall, PID: 1086
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sbi.mvicall/com.sbi.mvicall.SettingPageActivity}: android.view.InflateException: Binary XML file line #30: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
Caused by: android.view.InflateException: Binary XML file line #30: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:768)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:865)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:873)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
at android.view.LayoutInflater.inflate(LayoutInflater.java:525)
at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
at android.view.LayoutInflater.inflate(LayoutInflater.java:378)
at android.support.v7.app.k.b(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.c.setContentView(AppCompatActivity.java:139)
at com.sbi.mvicall.SettingPageActivity.onCreate(SettingPageActivity.java:11)
at android.app.Activity.performCreate(Activity.java:6955)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
在您的 XML 布局中,将 view
更改为 View
(使用大写字母)
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/grey_e4e4e4"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"/>
执行此 activity 时得到 "Attempt to invoke virtual method 'boolean java.lang.String.equals (java.lang.Object)' on a null object reference",可能我的布局中缺少某些内容。请帮我解决这个问题,这是我目前的 layout.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical"
tools:context="com.sbi.mvicall.SettingPageActivity">
<include
android:id="@+id/toolbar"
layout="@layout/view_toolbar"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/language"
android:textSize="16sp"/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/language"
android:spinnerMode="dropdown"
android:layout_weight="1">
</Spinner>
</LinearLayout>
<view
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/grey_e4e4e4"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/about"
android:id="@+id/about"
android:textSize="16sp"/>
</LinearLayout>
这是我的 layout.java :
package com.sbi.mvicall;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class SettingPageActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting_page);
}
}
这是错误日志:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sbi.mvicall, PID: 1086
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sbi.mvicall/com.sbi.mvicall.SettingPageActivity}: android.view.InflateException: Binary XML file line #30: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
Caused by: android.view.InflateException: Binary XML file line #30: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:768)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:865)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:873)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
at android.view.LayoutInflater.inflate(LayoutInflater.java:525)
at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
at android.view.LayoutInflater.inflate(LayoutInflater.java:378)
at android.support.v7.app.k.b(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.c.setContentView(AppCompatActivity.java:139)
at com.sbi.mvicall.SettingPageActivity.onCreate(SettingPageActivity.java:11)
at android.app.Activity.performCreate(Activity.java:6955)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
在您的 XML 布局中,将 view
更改为 View
(使用大写字母)
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/grey_e4e4e4"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"/>