getLastNonConfigurationInstance 总是返回 null [英] getLastNonConfigurationInstance always returning null

查看:31
本文介绍了getLastNonConfigurationInstance 总是返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HashMap myMap = (HashMap) getLastNonConfigurationInstance();

myMap 始终为空.getLastNonConfigurationInstance() 返回一个对象.我的地图有两个键符号"和名称".

myMap is always null. getLastNonConfigurationInstance() returns an object. My map has two keys "symbol" and "name".

public Object onRetainNonConfigurationInstance()
    {
        HashMap myMap = new HashMap();
        myMap.put("symbol", this.symbol);
        final Object data = myMap;
        return data;
    }

推荐答案

如果 getLastNonConfigurationInstance() 返回一个非空对象,那么 (HashMap) getLastNonConfigurationInstance() 将要么返回相同的对象(如果该对象是 HashMap),或者抛出 ClassCastException.

If getLastNonConfigurationInstance() returns a non-null object, then (HashMap) getLastNonConfigurationInstance() will either return the same object (if that object is a HashMap), or throw a ClassCastException.

您描述的情况是不可能的,除非您在 Java 的强制转换运算符中发现了一个长期隐藏的错误.提示:你没有.

The situation that you describe is not possible, not unless you've uncovered a long-hidden bug in Java's cast operator. Hint: you haven't.

验证 getLastNonConfigurationInstance() 实际上返回的是非空对象.验证 myMap 实际上是否为空.如果您使用调试器检查这些值,请尝试将它们打印到控制台.调试器有时会欺骗您,或者至少会误导您.

Verify that getLastNonConfigurationInstance() is actually returning a non-null object. Verify that myMap is actually null. If you're using a debugger to check those values, try printing them to the console instead. Debuggers can lie to you sometimes, or at least mislead.

这篇关于getLastNonConfigurationInstance 总是返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆