简体   繁体   English

Android尝试从空对象引用中的字段读取

[英]Android Attempt to read from field on a null object reference

I have an Activity with some Fragments used in a ViewPager , sometimes user sends me some crash report that i don't understand. 我有一个Activity的一些Fragments在使用ViewPager ,有时用户发送我有些崩溃报告,我不明白。

I can not produce this crash and most of the user have no problem at all. 我无法发生此崩溃,并且大多数用户完全没有问题。

The crash Log says: 崩溃日志中说:

java.lang.NullPointerException: Attempt to read from field 'com.mehdok.views.treeview.TreeNode$TreeNodeClickListener com.mehdok.mafatih.MainActivity.nodeClickListener2' on a null object reference

The nodeClickListener2 is in my main activity: nodeClickListener2在我的主要活动中:

public TreeNode.TreeNodeClickListener nodeClickListener2 = new TreeNode.TreeNodeClickListener()
{
    @Override
    public void onClick(TreeNode node, Object value)
    {
        TreeViewHolderNoBorder.TreeViewItem item = (TreeViewHolderNoBorder.TreeViewItem) value;
        if(node.isLeaf() && !item.nodeNavUri.equals(""))
        {
            openNodePage2(item);
        }
    }
};

And in fragmets i use it like this: 在碎片中,我像这样使用它:

tView.setDefaultNodeClickListener(((MainActivity) getActivity()).nodeClickListener2);

What can i do to prevent this crash? 我该怎么做以防止此崩溃?

tnx in advance. 提前发送。

That is possible, sometimes Activity will be recycled, when you call getActivity() in Fragment, it will return null. 这是可能的,有时Activity将被回收,当您在Fragment中调用getActivity()时,它将返回null。 so before you call getActivity() you can write code as below: 因此,在调用getActivity() ,可以编写如下代码:

if (isAdded()) {
        tView.setDefaultNodeClickListener(((MainActivity) getActivity()).nodeClickListener2);
        ...
    }

Where do you access your activity field from your fragment? 您从哪里访问片段中的活动字段? You should do it starting from onAttach() up until onDetach(). 您应该从onAttach()到onDetach()开始执行此操作。

If you try to access it too soon like in your constructor or in your fragment's onCreate(), this would explain the crash. 如果您试图像在构造函数中或片段的onCreate()中那样过早地访问它,这将解释崩溃。 Same if you try to access it after the fragment is detached from the Activity. 如果您将片段从活动中分离后尝试访问它,则相同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 空指针异常,“尝试从空对象引用上的字段读取” - Null pointer exception, "Attempt to read from field on a null object reference" Android ViewGroup 崩溃:尝试从空对象引用上的字段“int android.view.View.mViewFlags”读取 - Android ViewGroup crash: Attempt to read from field 'int android.view.View.mViewFlags' on a null object reference 尝试从 AndroidDevMetrics 中的字段读取空对象引用 - Attempt to read from field in AndroidDevMetrics on a null object reference 哪个引用变量为null-尝试从null对象引用上的字段'android.os.Handler android.support.v4.ama'中读取 - which reference variable is null - Attempt to read from field 'android.os.Handler android.support.v4.a.m.a' on a null object reference “尝试在空对象引用上读取字段'int android.content.pm.ApplicationInfo.targetSdkVersion'”异常 - “Attempt to read from field 'int android.content.pm.ApplicationInfo.targetSdkVersion' on a null object reference” exception 尝试从null对象引用上的字段'int android.media.CamcorderProfile.fileFormat'中读取 - Attempt to read from field 'int android.media.CamcorderProfile.fileFormat' on a null object reference NullPointerException:尝试从空对象引用上的字段'int android.app.Fragment.mContainerId'读取 - NullPointerException: Attempt to read from field 'int android.app.Fragment.mContainerId' on a null object reference 尝试从 null object 参考上的字段 'long android.graphics.Typeface.native_instance' 中读取 - Attempt to read from field 'long android.graphics.Typeface.native_instance' on a null object reference 尝试从字段 'int com.android.server.wm.Task.mTaskId' 读取 null object 参考 - Attempt to read from field 'int com.android.server.wm.Task.mTaskId' on a null object reference 使用导航抽屉时尝试从空对象引用上的字段'int android.support.v4.app.Fragment.mContainerId'读取 - Attempt to read from field 'int android.support.v4.app.Fragment.mContainerId' on a null object reference when using Navigation Drawer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM