アプリにスナックバーを表示するための次のコードがあります。
#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>
Android MおよびAPI 22より前でテストするまで、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: null オブジェクト参照 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): at 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(Native Method) 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(Native Method) 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(Native Method) 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() 呼び出しを削除して、効果があるかどうかを確認しようとしましたが、効果はありません。