我有以下代码用于在我的应用程序中显示 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):致命例外:主要 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:尝试在空对象引用上调用虚拟方法“int android.text.Layout.getLineCount()” 01-01 15:10:45.454 E/AndroidRuntime( 2482): 在 android.support.design.widget.Snackbar$SnackbarLayout.onMeasure(Snackbar.java:707) 01-01 15:10:45.454 E/AndroidRuntime(2482): 在 android.view.View.measure(View. java:18788) 01-01 15:10:45.454 E/AndroidRuntime(2482): 在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 01-01 15:10:45.454 E/AndroidRuntime(2482): 在android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:610) 01-01 15:10:45。454 E/AndroidRuntime(2482):在 android.widget.LinearLayout.measureVertical(LinearLay01-01 15:10:45.454 E/AndroidRuntime(2482):在 android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:677 ) 01-01 15:10:45.454 E/AndroidRuntime(2482): 在 android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715) 01-01 15:10:45.454 E/AndroidRuntime(2482): 在 android.widget .RelativeLayout.onMeasure(RelativeLayout.java:461) 01-01 15:10:45.454 E/AndroidRuntime(2482): 在 android.view.View.measure(View.java:18788) 01-01 15:10:45.454 E /AndroidRuntime(2482):在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)01-01 15:10:45.454 E/AndroidRuntime(2482):在 android.widget.FrameLayout.onMeasure(FrameLayout.java:194 ) 01-01 15:10:45.454 E/AndroidRuntime(2482):在 android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135) 01-01 15:10:45.454 E/AndroidRuntime(2482): 在 android.view.View.measure(View.java:18788) 01 -01 15:10:45.454 E/AndroidRuntime(2482): 在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 01-01 15:10:45.454 E/AndroidRuntime(2482): 在 android.widget.LinearLayout .measureChildBeforeLayout(LinearLayout.java:1465) 01-01 15:10:45.454 E/AndroidRuntime(2482): 在 android.widget.LinearLayout.measureVertical(LinearLayout.java:748) 01-01 15:10:45.454 E/AndroidRuntime (2482): 在 android.widget.LinearLayout.onMeasure(LinearLayout.java:630) 01-01 15:10:45.454 E/AndroidRuntime(2482): 在 android.view.View.measure(View.java:18788) 01 -01 15:10:45.454 E/AndroidRuntime(2482):在 android.view.ViewGroup。measureChildWithMargins(ViewGroup.java:5951) 01-01 15:10:45.454 E/AndroidRuntime(2482): 在 android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 01-01 15:10:45.454 E/AndroidRuntime( 2482): 在 android.view.View.measure(View.java:18788) 01-01 15:10:45.454 E/AndroidRuntime(2482): 在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 01- 01 15:10:45.454 E/AndroidRuntime(2482):在 android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465) 01-01 15:10:45.454 E/AndroidRuntime(2482):在 android.widget.LinearLayout。 measureVertical(LinearLayout.java:748) 01-01 15:10:45.454 E/AndroidRuntime(2482): 在 android.widget.LinearLayout.onMeasure(LinearLayout.java:630) 01-01 15:10:45.454 E/AndroidRuntime( 2482):在 android.view.View.measure(View.java:18788)01-01 15:10:45。454 E/AndroidRuntime(2482):在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)01-01 15:10:45.454 E/AndroidRuntime(2482):在 android.widget.FrameLayout.onMeasure(FrameLayout.java :194) 01-01 15:10:45.454 E/AndroidRuntime(2482): 在 com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643) 01-01 15:10:45.454 E/AndroidRuntime (2482):在 android.view.View.measure(View.java:18788)01-01 15:10:45.454 E/AndroidRuntime(2482):在 android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)01 -01 15:10:45.454 E/AndroidRuntime(2482):在 android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)01-01 15:10:45.454 E/AndroidRuntime(2482):在 android.view.ViewRootImpl .performTraversals(ViewRootImpl.java:1452)01-01 15:10:45.454 E/AndroidRuntime(2482):在 android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107) 01-01 15:10:45.454 E/AndroidRuntime(2482): 在 android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013) 01-01 15:10:45.454 E/AndroidRuntime(2482):在 android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)01-01 15:10:45.454 E/AndroidRuntime(2482):在 android.view.Choreographer .doCallbacks(Choreographer.java:670)01-01 15:10:45.454 E/AndroidRuntime(2482):在 android.view.Choreographer.doFrame(Choreographer.java:606)01-01 15:10:45.454 E/AndroidRuntime (2482):在 android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)01-01 15:10:45.454 E/AndroidRuntime(2482):在 android.os.Handler.handleCallback(Handler.java:739 ) 01-01 15:10:45.454 E/AndroidRuntime(2482): 在 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):在 android.app.ActivityThread.main(ActivityThread.java:5417)01-01 15:10:45.454 E/AndroidRuntime(2482):在 java.lang.reflect.Method.invoke(本机方法)01 -01 15:10:45.454 E/AndroidRuntime(2482):在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)01-01 15:10:45.454 E/AndroidRuntime(2482):在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)454 E/AndroidRuntime(2482):在 java.lang.reflect.Method.invoke(本机方法)01-01 15:10:45.454 E/AndroidRuntime(2482):在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller。运行(ZygoteInit.java:726)01-01 15:10:45.454 E/AndroidRuntime(2482):在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)454 E/AndroidRuntime(2482):在 java.lang.reflect.Method.invoke(本机方法)01-01 15:10:45.454 E/AndroidRuntime(2482):在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller。运行(ZygoteInit.java:726)01-01 15:10:45.454 E/AndroidRuntime(2482):在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
我尝试从我的代码中删除 Dispose() 调用,看看这是否会产生影响,但它没有。