アプリ制作実習 〜ダイアログでハマったの巻〜
こんにちは
ブログ投稿二回目です。haneoです。
この12月から晴れて正社員で、今何をやっているかというと
人生で初めて1からAndroidアプリを作っています。
写真をポラロイド風に加工(下にコメントをつけられる!!)して、
【参考画像】
見易いアルバムのように管理できる…というものを考えているのですが、
納期が今月24日でそれまでに作り上げなくてはならないのであと…18日くらいですか?
既に巻き状態で冷や汗が止まりませんが、気にせずブログも更新していきたいと思います。
それでは昨日学んだことのまとめを
ダイアログ
このアプリで新規のアルバムを作るとき
タイトルを入力用のダイアログを出すプログラムを書きました。
以下
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_delete: Intent intentD = new Intent(this, HomeDeleteActivity.class); startActivity(intentD); return true; case R.id.menu_add_new: alertDialog = new AlertDialog.Builder(this) .setTitle("新規アルバムを作成します") .setView(titleInput) .setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton){ //OKボタン 新しいアルバムを作成 //Intent intentA = new Intent(this, PhotoNewActivity.class); //startActivity(intentA); } }) .setNegativeButton("Cancerl", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int whichButton){ //キャンセルボタン 戻る } }).show(); return true; default: return super.onOptionsItemSelected(item); }
ただ、これだと戻るなど二度目に表示するとIlligalStateExceptionが出てしまいます。
実は上の方でEditTextを作っていたのですが
これだとアラートダイアログを2回目以上呼び出すときは使い回す形になっていたみたいです。
ということで、
case R.id.menu_add_new: EditText titleInput = new EditText(this); alertDialog = new AlertDialog.Builder(this) .setTitle("新規アルバムを作成します") .setView(titleInput) .setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton){ //OKボタン 新しいアルバムを作成 //Intent intentA = new Intent(this, PhotoNewActivity.class); //startActivity(intentA); } }) .setNegativeButton("Cancerl", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int whichButton){ } }) .show();
caseの中でEditTextをつくったところIlligalStateExceptionは出なくなりました💦