今回、BroadcastReceiver受信後のダイアログ表示として実装した中でエラーが発生してしまい、原因が分からず手詰まりとなってしまいました。
エラーが発生した状況は
BroadcastReceiver受信後OnCreateDialogのダイアログを表示し、STOPボタンを押したときに次のダイアログ(ShowDialogAsync)を表示。
ShowDialogAsyncのダイアログ表示を行った際に1回目の表示はうまく動作するのですが、2回目以降表示されません。
最終的な動作としてShowDialogAsyncの繰り返し表示の条件としてAnswersが4になるまで正常に繰り返し表示を行う事です。
何度も似たことを質問してしまい申し訳ありませんがよろしくお願い致します。
###確認した流れ
while(await ShowDialogAsync())
を条件判定・実行1回目
ShowDialogAsync()を上から下まで実行
while文内の処理を実行
while(await ShowDialogAsync())
の条件判定・実行2回目
ShowDialogAsync()内の始めの処理var source = new TaskCompletionSource<bool>();
とLinearLayout layout_out = new LinearLayout(Activity) { Orientation = Orientation.Vertical };
を実行した後、if(Answers == 4)
のコメントの部分までスキップされエラーとなりました。
ここで以下のエラーコードが吐かれました。
###エラーコード抜粋
UNHANDLED EXCEPTION:
02-05 12:29:52.313 I/MonoDroid( 3405): Java.Lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
C#
1[Activity(Label = "Alarm_Game")] 2 class AlertDialogFragment : Android.Support.V4.App.DialogFragment 3 { 4 //答え 5 public int Answer = 0; 6 //式 7 public string Formula = ""; 8 //何問目か 9 public int Answers = 1; 10 public int Nanido = 1; 11 12 public override Dialog OnCreateDialog(Bundle savedInstanceState) 13 { 14 Game_Process gp = new Game_Process(); 15 AlertDialog.Builder builder = new AlertDialog.Builder(Activity); 16 17 //レイアウトを作成 18 LayoutInflater inflater = (LayoutInflater)Activity.GetSystemService(Context.LayoutInflaterService); 19 View content = inflater.Inflate(Resource.Layout.TestLayout, null); 20 21 View cnt = inflater.Inflate(Resource.Layout.TestLayout, null); 22 Button alarm_stop = cnt.FindViewById<Button>(Resource.Id.al_stop); 23 Button alarm_snz = cnt.FindViewById<Button>(Resource.Id.snz_btn); 24 25 builder.SetPositiveButton("STOP", async (sender, e) => 26 { 27 //ランダム値生成と代入 28 gp.Randoms(Nanido); 29 Formula = gp.formula; 30 Answer = gp.answer; 31 32 while (await ShowDialogAsync()) 33 { 34 Console.WriteLine("await内"); 35 gp.Randoms(Nanido); 36 Formula = gp.formula; 37 Answer = gp.answer; 38 } 39 }); 40 41 builder.SetView(content); 42 Dialog dialog = builder.Create(); 43 dialog.SetCanceledOnTouchOutside(false); 44 return dialog; 45 46 } 47 48 public AlertDialog.Builder builder; 49 50 public Task<bool> ShowDialogAsync() 51 { 52 var source = new TaskCompletionSource<bool>(); 53 54 //レイアウト 55 LinearLayout layout_out = new LinearLayout(Activity) { Orientation = Orientation.Vertical }; 56 layout_out.SetGravity(GravityFlags.Left); 57 var input_ans = new EditText(Activity); 58 input_ans.InputType = Android.Text.InputTypes.ClassNumber; 59 layout_out.AddView(new TextView(Activity) { Text = "3個中" + Answers + "個目", TextSize = 30 }); 60 layout_out.AddView(new TextView(Activity) { Text = Formula, TextSize = 30 }); 61 layout_out.AddView(input_ans); 62 Console.WriteLine("builder:"+builder); 63 64 if (builder == null) 65 { 66 builder = new AlertDialog.Builder(Activity); 67 builder.SetTitle("回答しなさい"); 68 builder.SetView(layout_out); 69 builder.SetPositiveButton("OK", (s, a) => 70 { 71 72 int input_i = 0; 73 if (input_ans.Text == null) 74 { 75 //NULLの場合何もしない 76 } 77 else 78 { 79 string input_s = input_ans.Text; 80 input_i = int.Parse(input_s); 81 } 82 if (input_i == Answer) 83 { 84 Answers++; 85 } 86 if (Answers == 4) 87 { 88 source.SetResult(false); 89 Answers = 1; 90 } 91 else 92 { 93 source.SetResult(true); 94 } //ここのカッコまでスキップ 95 }); 96 97 } 98 99 builder.Show(); 100 101 return source.Task; 102 } 103 } 104
あなたの回答
tips
プレビュー