0

リスナーを追加したいのですが、アプリがクラッシュするダイアログを作成します。何が問題になっていますか?

private void Info(){

        textview = (TextView) findViewById(R.id.textView1);

        LayoutInflater li = LayoutInflater.from(this);
        View view = li.inflate(R.layout.info, null);

        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(view).create().show();

        buttonInfo = (Button)findViewById(R.id.buttonInfo);

        buttonInfo.setOnClickListener(new View.OnClickListener() {
              public void onClick(View view) { 

              }
            });
4

2 に答える 2

2

交換

 buttonInfo = (Button) view.findViewById(R.id.buttonInfo);

buttonInfo = (Button) findViewById(R.id.buttonInfo);

最終コード

private void Info(){

        textview = (TextView) findViewById(R.id.textView1);

        LayoutInflater li = LayoutInflater.from(this);
        View view = li.inflate(R.layout.info, null);

        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(view).create().show();

        buttonInfo = (Button) view.findViewById(R.id.buttonInfo);

        buttonInfo.setOnClickListener(new View.OnClickListener() {
              public void onClick(View view) { 

              }
            });
于 2012-11-21T13:17:48.183 に答える
1

ボタンはダイアログのレイアウトの中にあると思います。

その場合は、交換してみてください

buttonInfo = (Button)findViewById(R.id.buttonInfo);

buttonInfo = (Button) view.findViewById(R.id.buttonInfo);

また、作成したAlertDialogを(ビルダーではなく)保存したい場合は、次の後にそれを閉じる必要があります。

    final AlertDialog dialog = new AlertDialog.Builder(this).setView(view).show();

    buttonInfo = (Button) findViewById(R.id.buttonInfo);

    buttonInfo.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            dialog.dismiss();
        }
    });
于 2012-11-21T13:16:39.647 に答える