Android - Snackbar - 在调用Show()后调用FATAL异常

时间:2016-01-01 14:17:56

标签: c# android xamarin android-snackbar

我有以下代码在我的应用程序中显示Snackbar。

#region Snackbar

        private const int SnackbarDuration = 1100;

        /// <summary>
        /// Shows a snackbar. Counts with the fact that there is a CoordinatorLayout with ID coordinatorLayout in your activity layout.
        /// </summary>
        /// <param name="activity"></param>
        /// <param name="text"></param>
        /// <param name="duration"></param>
        public static void ShowSnackbar(Activity activity, string text, int duration = SnackbarDuration)
        {
            StyleAndShowSnackbar(activity, text, duration);
        }

        /// <summary>
        /// Shows a snackbar. Counts with the fact that there is a CoordinatorLayout with ID coordinatorLayout in your activity layout.
        /// </summary>
        /// <param name="activity"></param>
        /// <param name="stringId"></param>
        /// <param name="duration"></param>
        public static void ShowSnackbar(Activity activity, int stringId, int duration = SnackbarDuration)
        {
            StyleAndShowSnackbar(activity, stringId, duration);
        }

        private static void StyleAndShowSnackbar(Activity activity, object stringOrStringId, int duration)
        {
            var snackbarLayout = activity.FindViewById<CoordinatorLayout>(Resource.Id.coordinatorLayout);
            if (snackbarLayout == null) return;

            Snackbar snackbar;
            if (stringOrStringId is int)
                snackbar = Snackbar.Make(snackbarLayout, (int)stringOrStringId, duration);
            else
                snackbar = Snackbar.Make(snackbarLayout, (string)stringOrStringId, duration);

            snackbar.StyleSnackbar();
            snackbar.Show();
            snackbar.Dispose();
            snackbarLayout.Dispose();
        }

        private static void StyleSnackbar(this Snackbar snackbar)
        {
            var snackbarView = (ViewGroup)snackbar.View;
            snackbarView.SetBackgroundColor(Color.LightGray);
            var snackbarTextView = snackbarView.FindViewById<TextView>(Resource.Id.snackbar_text);
            snackbarTextView.SetTextColor(Color.Black);

            snackbarView.Dispose();
            snackbarTextView.Dispose();
        }

        #endregion

这是从我的代码中引用的协调器布局(请注意,这包含在我的布局中 - 它没有单独使用):

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true">
</android.support.design.widget.CoordinatorLayout>

一切都在API 22上运行良好,直到我在Android M和早于API 22测试它(可能不是主要原因)。在调用Show()之后,App崩溃并出现以下异常。

  

01-01 15:10:45.454 E / AndroidRuntime(2482):致命异乎寻常:主要   01-01 15:10:45.454 E / AndroidRuntime(2482):流程:   com.psonar.android,PID:2482 01-01 15:10:45.454 E / AndroidRuntime(   2482):java.lang.NullPointerException:尝试调用虚拟   方法&#39; int android.text.Layout.getLineCount()&#39;在null对象上   参考01-01 15:10:45.454 E / AndroidRuntime(2482):at   android.support.design.widget.Snackbar $ SnackbarLayout.onMeasure(Snackbar.java:707)   01-01 15:10:45.454 E / AndroidRuntime(2482):at   android.view.View.measure(View.java:18788)01-01 15:10:45.454   E / AndroidRuntime(2482):at   android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)   01-01 15:10:45.454 E / AndroidRuntime(2482):at   android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:610)   01-01 15:10:45.454 E / AndroidRuntime(2482):at   android.widget.LinearLayout.measureVertical(LinearLay01-01   15:10:45.454 E / AndroidRuntime(2482):at   android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:677)   01-01 15:10:45.454 E / AndroidRuntime(2482):at   android.widget.RelativeLayout.measureChildHorizo​​ntal(RelativeLayout.java:715)   01-01 15:10:45.454 E / AndroidRuntime(2482):at   android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)01-01   15:10:45.454 E / AndroidRuntime(2482):at   android.view.View.measure(View.java:18788)01-01 15:10:45.454   E / AndroidRuntime(2482):at   android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)   01-01 15:10:45.454 E / AndroidRuntime(2482):at   android.widget.FrameLayout.onMeasure(FrameLayout.java:194)01-01   15:10:45.454 E / AndroidRuntime(2482):at   android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)   01-01 15:10:45.454 E / AndroidRuntime(2482):at   android.view.View.measure(View.java:18788)01-01 15:10:45.454   E / AndroidRuntime(2482):at   android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)   01-01 15:10:45.454 E / AndroidRuntime(2482):at   android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)   01-01 15:10:45.454 E / AndroidRuntime(2482):at   android.widget.LinearLayout.measureVertical(LinearLayout.java:748)   01-01 15:10:45.454 E / AndroidRuntime(2482):at   android.widget.LinearLayout.onMeasure(LinearLayout.java:630)01-01   15:10:45.454 E / AndroidRuntime(2482):at   android.view.View.measure(View.java:18788)01-01 15:10:45.454   E / AndroidRuntime(2482):at   android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)   01-01 15:10:45.454 E / AndroidRuntime(2482):at   android.widget.FrameLayout.onMeasure(FrameLayout.java:194)01-01   15:10:45.454 E / AndroidRuntime(2482):at   android.view.View.measure(View.java:18788)01-01 15:10:45.454   E / AndroidRuntime(2482):at   android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)   01-01 15:10:45.454 E / AndroidRuntime(2482):at   android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)   01-01 15:10:45.454 E / AndroidRuntime(2482):at   android.widget.LinearLayout.measureVertical(LinearLayout.java:748)   01-01 15:10:45.454 E / AndroidRuntime(2482):at   android.widget.LinearLayout.onMeasure(LinearLayout.java:630)01-01   15:10:45.454 E / AndroidRuntime(2482):at   android.view.View.measure(View.java:18788)01-01 15:10:45.454   E / AndroidRuntime(2482):at   android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)   01-01 15:10:45.454 E / AndroidRuntime(2482):at   android.widget.FrameLayout.onMeasure(FrameLayout.java:194)01-01   15:10:45.454 E / AndroidRuntime(2482):at   com.android.internal.policy.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:2643)   01-01 15:10:45.454 E / AndroidRuntime(2482):at   android.view.View.measure(View.java:18788)01-01 15:10:45.454   E / AndroidRuntime(2482):at   android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)01-01   15:10:45.454 E / AndroidRuntime(2482):at   android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)   01-01 15:10:45.454 E / AndroidRuntime(2482):at   android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)   01-01 15:10:45.454 E / AndroidRuntime(2482):at   android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)01-01   15:10:45.454 E / AndroidRuntime(2482):at   android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6013)   01-01 15:10:45.454 E / AndroidRuntime(2482):at   android.view.Choreographer $ CallbackRecord.run(Choreographer.java:858)   01-01 15:10:45.454 E / AndroidRuntime(2482):at   android.view.Choreographer.doCallbacks(Choreographer.java:670)01-01   15:10:45.454 E / AndroidRuntime(2482):at   android.view.Choreographer.doFrame(Choreographer.java:606)01-01   15:10:45.454 E / AndroidRuntime(2482):at   android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:844)   01-01 15:10:45.454 E / AndroidRuntime(2482):at   android.os.Handler.handleCallback(Handler.java:739)01-01 15:10:45.454   E / AndroidRuntime(2482):at   android.os.Handler.dispatchMessage(Handler.java:95)01-01 15:10:45.454   E / AndroidRuntime(2482):在android.os.Looper.loop(Looper.java:148)   01-01 15:10:45.454 E / AndroidRuntime(2482):at   android.app.ActivityThread.main(ActivityThread.java:5417)01-01   15:10:45.454 E / AndroidRuntime(2482):at   java.lang.reflect.Method.invoke(Native Method)01-01 15:10:45.454   E / AndroidRuntime(2482):at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)   01-01 15:10:45.454 E / AndroidRuntime(2482):at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

我尝试从我的代码中删除Dispose()调用,以查看它是否会产生影响,但事实并非如此。

1 个答案:

答案 0 :(得分:1)

简单清理解决方案并完全卸载应用程序并重新安装就可以了。

我希望它更多,但最初它起作用然后停止然后清理并重新安装解决它(对于Android M我必须删除并重新安装设备图像)。