Android - Snackbar - 在调用 Show() 后因致命异常而崩溃
Android - Snackbar - Crashing with FATAL Exception after Show() is called
我有以下用于在我的应用程序中显示 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() 后应用程序崩溃并出现以下异常。
01-01 15:10:45.454 E/AndroidRuntime( 2482): FATAL EXCEPTION: main
01-01 15:10:45.454 E/AndroidRuntime( 2482): Process:
com.psonar.android, PID: 2482 01-01 15:10:45.454 E/AndroidRuntime(
2482): java.lang.NullPointerException: Attempt to invoke virtual
method 'int android.text.Layout.getLineCount()' on a null object
reference 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.measureChildHorizontal(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): at 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() 调用以查看它是否会产生影响,但它没有。
简单清理解决方案并完全卸载应用程序并重新安装就可以了。
我希望它能有更多的东西,但起初它能工作,然后它停止了,然后清理并重新安装解决了它(对于 Android M,我不得不删除并重新安装设备映像)。
我有以下用于在我的应用程序中显示 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() 后应用程序崩溃并出现以下异常。
01-01 15:10:45.454 E/AndroidRuntime( 2482): FATAL EXCEPTION: main 01-01 15:10:45.454 E/AndroidRuntime( 2482): Process: com.psonar.android, PID: 2482 01-01 15:10:45.454 E/AndroidRuntime( 2482): java.lang.NullPointerException: Attempt to invoke virtual method 'int android.text.Layout.getLineCount()' on a null object reference 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.measureChildHorizontal(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): at 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() 调用以查看它是否会产生影响,但它没有。
简单清理解决方案并完全卸载应用程序并重新安装就可以了。
我希望它能有更多的东西,但起初它能工作,然后它停止了,然后清理并重新安装解决了它(对于 Android M,我不得不删除并重新安装设备映像)。