以前、ダイアログの表示を繰り返す際にはawait/asyncを用いると可能ということで実装し、実行する事ができたのですが、少し仕様変更のため利用方法が変わり、OnReceiveが受信した際に実行する形に変わりました。実装してみたはいいのですが、ダイアログの表示はActivityを継承し、OnReceiveメソッドはBroadcastReceiverを継承するため別々のクラスに分けなければならないと考え、クラスを分けました。実装したところ、Unhandled Exception:が発生してしまい行き詰まってしまいました。
出来る限り特定できたことは[DialogAsync]クラスの[ShowDialogAsync]メソッド内[var layout_out = new LinearLayout(this) { Orientation = Orientation.Vertical };]の部分にブレークポイントを設定したところこの部分を実行した際に
Unhandled Exception:
Java.Lang.NullPointerException: <Timeout exceeded getting exception details>
というエラーが発生しました。
最終的な動作としてOnReceiveが受信したらダイアログを3回繰り返し表示・入力及び入力値を比較できるという動作を目標にしています。
C#
1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5 6using Android.App; 7using Android.Content; 8using Android.OS; 9using Android.Runtime; 10using Android.Views; 11using Android.Widget; 12using System.Threading.Tasks; 13using Java.Lang; 14 15namespace Alarm_Game 16 [BroadcastReceiver] 17 class ReceivedActivity : BroadcastReceiver 18 { 19 Process gp = new Process(); 20 DialogAsync da = new DialogAsync(); 21 22 public override async void OnReceive(Context context, Intent intent) 23 { 24 //エラー発生時のキャッチ? 25 AndroidEnvironment.UnhandledExceptionRaiser += (s, e) => { e.Handled = true; }; 26 27 Console.WriteLine("OnReceive"); 28 //処理記載 29 30 //ダイアログ表示 31 await da.StartM(); 32 } 33 34 } 35 [Activity(Label ="Alarm_Game")] 36 public class DialogAsync : Activity 37 { 38 Process gp = new Process(); 39 //答え 40 public int Answer = 0; 41 //式 42 public string Formula = ""; 43 //何問目か 44 public int Answers = 1; 45 46 public int Nanido = 1; 47 48 49 50 public async Task StartM() 51 { 52 53 gp.Randoms(Nanido); 54 Formula = gp.formula; 55 Answer = gp.answer; 56 57 await Task.Run(async () => 58 { 59 while (await ShowDialogAsync()) 60 { 61 gp.Randoms(Nanido); 62 Formula = gp.formula; 63 Answer = gp.answer; 64 } 65 }); 66 } 67 68 public Task<bool> ShowDialogAsync() 69 { 70 var source = new TaskCompletionSource<bool>(); 71 72 //レイアウト 73 var layout_out = new LinearLayout(this) { Orientation = Orientation.Vertical }; 74 layout_out.SetGravity(GravityFlags.Left); 75 var input_ans = new EditText(this); 76 input_ans.InputType = Android.Text.InputTypes.ClassNumber; 77 layout_out.AddView(new TextView(this) { Text = "3個中" + Answers + "個目", TextSize = 30 }); 78 layout_out.AddView(new TextView(this) { Text = Formula, TextSize = 30 }); 79 layout_out.AddView(input_ans); 80 81 //ダイアログ 82 var builder = new AlertDialog.Builder(this); 83 builder.SetTitle("回答せよ") 84 .SetView(layout_out) 85 .SetPositiveButton("OK", (s, a) => 86 { 87 int input_i = 0; 88 if (input_ans == null) 89 { 90 //答えが入力されていないときは初期値をそのまま利用し何もしない 91 } 92 else 93 { 94 string input_s = input_ans.Text; 95 input_i = int.Parse(input_s); 96 } 97 98 if (input_i == Answer) 99 { 100 Answers++; 101 } 102 103 104 if (Answers == 4) 105 { 106 source.SetResult(false); 107 Answers = 1; 108 } 109 else 110 { 111 source.SetResult(true); 112 } 113 }); 114 builder.Show(); 115 return source.Task; 116 } 117 } 118 119 }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/01/25 15:50