API 15上的SupportAppCompat-v7库中的NullPointerException

时间:2015-06-04 17:14:57

标签: android nullpointerexception android-support-library

在SupportAppCompat库中抛出NullPointerException。它似乎只发生在API 15(IceCreamSandwich)上运行的设备上。从16到22的API版本没有任何问题。

关于什么可能导致这次崩溃的任何想法?

感谢您的时间和可能的答案!

这是堆栈跟踪:

\documentclass[aspectratio=43,12pt]{beamer}\usetheme{Goettingen}

\begin{document}

\begin{frame}%%     1
\begin{center}
\Huge Thank You!
\end{center}
\end{frame}

\begin{frame}%%     2
\begin{center}
{\fontsize{40}{50}\selectfont Thank You!}
\end{center}
\end{frame}

\end{document}

2 个答案:

答案 0 :(得分:4)

经过大量时间寻找答案后,我找到了问题的根源。

我在MenuItem中使用了LayerDrawable,显然,在API 15上设置一次后,LayerDrawable无法修改。修改它会导致崩溃。

我希望如果有人遇到这个问题,这会有所帮助。

答案 1 :(得分:1)

@RMatt答案是正确的,如果你试图改变LayerDrawable并以编程方式进行更改,你将最终陷入上述崩溃。

以下是我为自己的案例解决问题的示例。

public static void setRoundedCornerDrawableInLayerList(@NonNull Context context, @NonNull View view, int layerIndex, float radius) {
        Drawable background = view.getBackground();
        if (background != null && background instanceof LayerDrawable) {
            LayerDrawable layers = (LayerDrawable) background.mutate();
            Drawable d = layers.getDrawable(0);
            if (d instanceof BitmapDrawable) {
                int id = layers.getId(layerIndex);
                BitmapDrawable bitmapDrawable = (BitmapDrawable) d;
                RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(context.getResources(), bitmapDrawable.getBitmap());
                roundedBitmapDrawable.setCornerRadius(radius);
                if (isJB()) {
                    layers.setDrawableByLayerId(id, roundedBitmapDrawable);
                    layers.invalidateSelf();
                } else {
                    int count = layers.getNumberOfLayers();
                    Drawable[] layerArray = new Drawable[count];
                    for (int i = 0; i < count; i++) {
                        if (i == layerIndex) {
                            layerArray[i] = roundedBitmapDrawable;
                        } else {
                            layerArray[i] = layers.getDrawable(i);
                        }
                    }
                    LayerDrawable layerDrawable = new LayerDrawable(layerArray);
                    setBackgroundDrawable(view, layerDrawable);
                }
            }

        }
    }

你可以看到,对于JellyBean之上的版本,我只是改变了drawable图层并用另一个版本替换了drawable,但对于较低版本,我从现有图层创建了新的LayerDrawable