且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

NullPointerException异常的SuggestionSpan

更新时间:2023-11-19 12:13:10

在连日侦查,事实证明,getResources()。getConfiguration()。区域设置为空。我不知道这是否是特定于某些版本的Andr​​oid的问题/某些设备(我的是Nexus S的运行4.0.3)。

After days of investigation, it turns out that getResources().getConfiguration().locale is null. I am not sure if it is a problem specific to some versions of Android / some devices (mine is Nexus S running 4.0.3).

但我通过检查活动的构造函数的工作解决此问题(因为给予SuggestionSpan的背景是我的活动)的区域是否为空,然后将其设置为默认的,如果它是。

But I worked around this issue by checking in the constructor of the Activity (since the context given to the SuggestionSpan is my Activity) whether the locale is null, and then set it to the default one if it is.

    // to prevent a weird bug where locale is null
    Configuration config = getResources().getConfiguration();
    if (config.locale == null)
        config.locale = Locale.getDefault();

这就是我所做的。我敢肯定它不是,虽然***的办法。任何人有任何更多的想法?

That's what I did. I am quite sure it's not the best way though. Anyone have any more ideas?