今回はカスタムダイアログの中身を動的に書き換えるやり方だよ。
普通にやったら(書き換えたいTextViewのidが R.id.hoge カスタムダイアログのレイアウトが R.layout.dialog の場合)
LayoutInflater inflater = LayoutInflater.from(this);
View layout = inflater.inflate(R.layout.dialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
〜中略〜
TextView tv = (TextView)findViewById(R.id.hoge);
tv.setText("書き換えたい内容がここ");
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
こんな感じでやると思うけど、これだと
tv.setText("書き換えたい内容がここ"); の部分が
java.lang.NullPointerException になるんじゃないかな、きっと。
そうなった人はこれで解決。
解決法
LayoutInflater inflater = LayoutInflater.from(this);
View layout = inflater.inflate(R.layout.dialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
〜中略〜
TextView tv = (TextView)layout.findViewById(R.id.hoge);
tv.setText("書き換えたい内容がここ");
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
TextView tv = (TextView)findViewById(R.id.hoge);
を
TextView tv = (TextView)layout.findViewById(R.id.hoge);
にしましょうね。