NPE 同时膨胀布局(尝试在空对象引用上调用虚拟方法'boolean java.lang.String.equals(java.lang.Object)')
Posted
技术标签:
【中文标题】NPE 同时膨胀布局(尝试在空对象引用上调用虚拟方法\'boolean java.lang.String.equals(java.lang.Object)\')【英文标题】:NPE while inflating layout (Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference)NPE 同时膨胀布局(尝试在空对象引用上调用虚拟方法'boolean java.lang.String.equals(java.lang.Object)') 【发布时间】:2016-03-19 11:30:16 【问题描述】:当我尝试在活动中使用ScrollView
时,我不断收到java.lang.NullPointerException
。
奇怪的是,我在其他活动中使用了完全相同的Scrollview
设置。突然之间,我遇到了错误。我已经尝试清理项目并重建。
我已经尝试了旧的重启程序。我今天学到了很多关于空指针异常的知识,但我一直无法弄清楚为什么我总是遇到渲染问题。
这是在 XML 中抛出的 java.lang.NullPointerException
:
java.lang.NullPointerException
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:738)
at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:149)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:835)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:70)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:811)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:838)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:70)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:811)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:838)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:70)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:811)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:223)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:426)
at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:350)
at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:510)
at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:498)
at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:888)
at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:498)
at com.android.tools.idea.rendering.RenderTask.access$600(RenderTask.java:72)
at com.android.tools.idea.rendering.RenderTask$3.call(RenderTask.java:610)
at com.android.tools.idea.rendering.RenderTask$3.call(RenderTask.java:607)
at com.android.tools.idea.rendering.RenderService.runRenderAction(RenderService.java:362)
at com.android.tools.idea.rendering.RenderTask.render(RenderTask.java:607)
at com.android.tools.idea.rendering.RenderTask.render(RenderTask.java:629)
at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager.doRender(AndroidLayoutPreviewToolWindowManager.java:652)
at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager.access$1700(AndroidLayoutPreviewToolWindowManager.java:80)
at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager$7$1.run(AndroidLayoutPreviewToolWindowManager.java:594)
at com.intellij.openapi.progress.impl.CoreProgressManager$2.run(CoreProgressManager.java:152)
at com.intellij.openapi.progress.impl.CoreProgressManager.registerIndicatorAndRun(CoreProgressManager.java:452)
at com.intellij.openapi.progress.impl.CoreProgressManager.executeProcessUnderProgress(CoreProgressManager.java:402)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:54)
at com.intellij.openapi.progress.impl.CoreProgressManager.runProcess(CoreProgressManager.java:137)
at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager$7.run(AndroidLayoutPreviewToolWindowManager.java:589)
at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:320)
at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:310)
at com.intellij.util.ui.update.MergingUpdateQueue$2.run(MergingUpdateQueue.java:254)
at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:269)
at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:227)
at com.intellij.util.ui.update.MergingUpdateQueue.run(MergingUpdateQueue.java:217)
at com.intellij.util.concurrency.QueueProcessor.runSafely(QueueProcessor.java:238)
at com.intellij.util.Alarm$Request$1.run(Alarm.java:351)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
所以,这些都不是我编写的代码。所以我深入到堆栈跟踪中,发现了这个:
java.lang.NullPointerException: 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:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
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:715)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378)
at android.app.Activity.setContentView(Activity.java:2145)
at com.mitchellpalermo.tutroubleshooting.ContactPage.onCreate(ContactPage.java:11)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
这指向 Contact.java 文件中的 第 11 行。看起来像这样(我在第 11 行附近放置了指标):
package com.mitchellpalermo.tutroubleshooting;
import android.app.Activity;
import android.os.Bundle;
public class Contact extends Activity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact); // line 11
我认为 第 11 行 没有问题。
现在,我无法弄清楚我的代码有什么问题。在 渲染问题 和 NullPointerExceptions
之间,我无法弄清楚我的代码到底出了什么问题。
这是一个 XML 在活动中的示例,其中ScrollView
不会引发任何错误:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:orientation="vertical"
tools:context="com.mitchellpalermo.tutroubleshooting.ComputerOrProjector">
<LinearLayout
android:layout_
android:layout_
android:layout_gravity="center"
android:orientation="vertical">
<TextView
style="@style/HeaderStyle"
android:layout_
android:layout_
android:layout_gravity="center"
android:text="@string/solutions"
android:textStyle="bold" />
<!-- A CardView that contains a TextView -->
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_
android:layout_
android:layout_gravity="center"
android:layout_margin="15dp"
card_view:cardBackgroundColor="#FFFFFF"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="5dp">
<LinearLayout
android:layout_
android:layout_
android:orientation="vertical">
<TextView
style="@style/Solution"
android:text="@string/pc_v_fifth" />
<View
android:layout_
android:layout_
android:background="@android:color/darker_gray" />
<TextView
style="@style/Solution"
android:text="@string/pc_v_first" />
<View
android:layout_
android:layout_
android:background="@android:color/darker_gray" />
<TextView
style="@style/Solution"
android:text="@string/pc_v_second" />
<View
android:layout_
android:layout_
android:background="@android:color/darker_gray" />
<TextView
style="@style/Solution"
android:text="@string/pc_v_third" />
<View
android:layout_
android:layout_
android:background="@android:color/darker_gray" />
<TextView
style="@style/Solution"
android:text="@string/pc_v_fourth" />
<View
android:layout_
android:layout_
android:background="@android:color/darker_gray" />
</LinearLayout>
</android.support.v7.widget.CardView>
<LinearLayout
android:layout_
android:layout_
android:orientation="horizontal">
<Button
style="@style/NextButton"
android:layout_
android:layout_
android:layout_gravity="center"
android:onClick="sendEmail"
android:text="@string/fixed"
android:textStyle="bold" />
<Button
style="@style/NextButton"
android:layout_
android:layout_
android:layout_gravity="center"
android:onClick="notFixedButton"
android:text="@string/not_fixed"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</ScrollView>
这是我收到错误的活动:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:fillViewport="false">
<LinearLayout
android:layout_
android:layout_
android:orientation="vertical">
<TextView
style="@style/HeaderStyle"
android:layout_
android:layout_
android:layout_gravity="center"
android:text="@string/still_not_fixed" />
<!-- A CardView that contains a TextView -->
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_
android:layout_
android:layout_gravity="center"
android:layout_margin="15dp"
card_view:cardBackgroundColor="#FFFFFF"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="5dp">
<TextView
android:layout_
android:layout_
android:text="@string/hector" />
<view
android:layout_
android:layout_
android:backgroundTint="@android:color/darker_gray" />
<TextView
android:layout_
android:layout_
android:text="@string/robert" />
<view
android:layout_
android:layout_
android:backgroundTint="@android:color/darker_gray" />
<TextView
android:layout_
android:layout_
android:text="@string/mitch" />
</android.support.v7.widget.CardView>
</LinearLayout>
</ScrollView>
有什么建议吗?并提前非常感谢您!
【问题讨论】:
您的代码中某处使用空字符串调用String.equals
。
这不是标准 NPE,因为它在系统膨胀操作中。因此,我重新打开了这个问题,因为它不是给定问题的重复。这是一个有效且有用的问题,并提供有用的答案。
检查解决方案:readyandroid.wordpress.com/… 以及更多readyandroid.wordpress.com
【参考方案1】:
将<view
更改为<View
,因为view
不是关于空 视图。它用于通过class
attr 定义的自定义视图,如下所示:
<view
android:layout_
android:layout_
class="com.your.package.YourCustomView" />
你得到了
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
因为LayoutInflater
试图解析class
attr:
LayoutInflater
源代码
//...
View createViewFromTag(View parent, String name, Context context, AttributeSet attrs,
boolean ignoreThemeAttr)
if (name.equals("view")) // line 724
name = attrs.getAttributeValue(null, "class"); // line 725
// Apply a theme wrapper, if allowed and one is specified.
if (!ignoreThemeAttr)
final TypedArray ta = context.obtainStyledAttributes(attrs, ATTRS_THEME);
final int themeResId = ta.getResourceId(0, 0);
if (themeResId != 0)
context = new ContextThemeWrapper(context, themeResId);
ta.recycle();
if (name.equals(TAG_1995)) // line 738
// Let's party like it's 1995!
return new BlinkLayout(context, attrs);
//...
在第 724 行检查您的标签是否为 view
并获得 true
在第 725 行,它尝试通过class
attr 获取课程并获取null
在第 738 行,它尝试检查 blink
标记并崩溃
另外,还有一个奇怪的事情,Android 开发者添加了彩蛋,你可以试试:
<view
android:layout_
android:layout_
class="blink">
<TextView
android:layout_
android:layout_
android:gravity="center"
android:text="Some text" />
</view>
【讨论】:
非常感谢,伊戈尔!我不确定我是如何错过没有大写2 天后,我解决了这个问题,使用 View 而不是 view
<View
android:layout_
android:layout_
android:background="#faf4f4"></View>
【讨论】:
以上是关于NPE 同时膨胀布局(尝试在空对象引用上调用虚拟方法'boolean java.lang.String.equals(java.lang.Object)')的主要内容,如果未能解决你的问题,请参考以下文章
致命异常: main ,尝试在空对象引用上调用虚拟方法 [重复]
尝试在空对象引用上调用虚拟方法 com.google.firebase.iid.FirebaseInstanceId.getInstanceId()'
NullPointerException 错误尝试在空对象引用上调用虚拟方法错误
AsyncTask +数据库尝试在空对象引用上调用虚拟方法[重复]
尝试在空对象引用上调用虚拟方法 'android.view.SurfaceHolder android.view.SurfaceView.getHolder()'