AlertDialogのOnClickイベント内のプライベートメンバーにアクセスします

StackOverflow https://stackoverflow.com/questions/3916506

  •  29-09-2019
  •  | 
  •  

質問

私はAndroid開発で非常に新しいです。

私はこれを持っています:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.message_user_registered)
       .setCancelable(false)
       .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
               Intent intent = 
                   new Intent(GameDescriptionActivity.this,
                              GameHomeActivity.class);

               Bundle extraData = new Bundle();
               extraData.putInt(Constants.GAME_ID, this.gameId);

               startActivity(intent);
           }
       });
AlertDialog alert = builder.create();
alert.show();

しかし、この行はうまくいきません:

extraData.putInt(Constants.GAME_ID, this.gameId);

アクセスできません this.gameId.

どうすればこれを修正できますか?

ありがとう。

役に立ちましたか?

解決

匿名の内側クラスを使用しているため、「この」は実際にそのクラスを指します。 「これ」を削除することにより、メインクラスのプライベートフィールドを参照できます。または書く:「nameofyourmainclass.this.gameid」

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top