当前位置:  开发笔记 > Android > 正文

在膨胀布局时尝试NPE(尝试在空对象引用上调用虚方法'boolean java.lang.String.equals(java.lang.Object)'

如何解决《在膨胀布局时尝试NPE(尝试在空对象引用上调用虚方法'booleanjava.lang.String.equals(java.lang.Object)'》经验,为你挑选了2个好方法。

java.lang.NullPointerException当我尝试ScrollView在活动中使用时,我会不断获得.

奇怪的是,我Scrollview在其他活动中使用了完全相同的设置.突然之间,我遇到了错误.我试过清理项目并重建.

而且我已经尝试了这个好老的重启程序.我今天学到了很多关于Null Pointer Exceptions的知识,但是我还没弄清楚为什么我一直在学习渲染问题.

这是java.lang.NullPointerException在XML上引发的:

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)

它指向第11行Contact.java文件.看起来像这样(我把指标放在第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不会抛出任何错误:



    

        

        
        

            

                

                

                

                

                

                

                

                


                

                

            
        

        

            

这是我收到错误的活动:




    

        

        
        

            

            

            

            

            

        

    


有什么建议?非常感谢你!



1> Igor Tyulkan..:

更改,因为view是不是空的观点.这是通过classattr 定义的自定义视图,如下所示:


而你得到了

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference

因为LayoutInflater试图解析classattr:

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行,它试图通过classattr 获取类并获取null

在第738行,它尝试检查blink标记并崩溃

此外,有一个奇怪的事情,Android开发人员添加了复活节彩蛋,你可以试试:


    



2> Sai Gopi N..:

2天后我解决了这个问题使用View而不是view


推荐阅读
刘美娥94662
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有