0

「ViewBreakout」というクラス内にアラート ダイアログをセットアップしています。ボタンをうまくセットアップしてボタンを作成しますが、インテントを追加しようとすると、 「The constructor Intent(new DialogInterface.OnClickListener() {}、クラス) は定義されていません" . それが提供する解決策は、intent();??? に一致するように引数を削除することです。

    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getContext());
    dialogBuilder.setTitle("UNLUCKY :(");
    dialogBuilder.setMessage("You lost all your lives");
    dialogBuilder.setPositiveButton("try again", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub

            Intent i = new Intent (this, main.class);//get error here
            startActivity(i);
        }
    });
    AlertDialog alertDialog = dialogBuilder.create();
    alertDialog.show();

クラス内のコードは次のとおりです。別のクラスのグローバル変数が 0 に等しい場合、alertDialog が設定されます。

これがクラスの私のコードです。クラス全体を見せて、足りないものがあるかどうかを確認しようと思いました

    /**
    * Displays a graphical view of the game of breakout
    */


    class ViewBreakout extends View implements OnTouchListener, Observer
    {
    // private static final long serialVersionUID = 1L;
    private ControllerBreakout breakoutController;
    private GameObject       ball;
    private GameObject[]     bricks;
    private GameObject       bat;
    private int              score;
    private long             frames = 0;
    private Paint            paint  = new Paint();
    private boolean isBall = true;



    public ViewBreakout(Context context)
    {
    super(context);
    Debug.trace("View Breakout");
    setFocusable(true);
    setFocusableInTouchMode(true);      //

    this.setOnTouchListener(this);      // Take touch actions

    paint.setColor(Color.BLACK);    // Paint colour
    paint.setAntiAlias(true);       // Better quality
    if ( W < 600)
     paint.setTextSize(30);        // Text size
    else
    paint.setTextSize(40);


    }

    /**
    * Code called to draw the current state of the game Uses 
    *    paint.setColor -- set paint colour
    *     drawRect:      -- Draw rectangle 
    *    setPaint:      -- Colour used 
    *    drawText:      -- Write string on display
    */
    @Override
    public void onDraw(Canvas canvas)
    {
    frames++;


     paint.setColor(Color.DKGRAY);    // Paint colour
     canvas.drawRect(0, 0, W, H, paint);

     //if lives is 0 then display message
     if(LIVES <= 0){
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getContext());
    dialogBuilder.setTitle("UNLUCKY :(");
    dialogBuilder.setMessage("You lost all your lives");
    dialogBuilder.setPositiveButton("try again", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub

            Intent i = new Intent (this, main.class);
            startActivity(i);
        }
    });
    AlertDialog alertDialog = dialogBuilder.create();
    alertDialog.show();

}
4

2 に答える 2

1

インテントは、別のクラスである inner class 内に作成されOnClickListenerます。したがって、" this" は匿名内部クラスのインスタンスを指しますOnClickListener

これを次のように変更します。

Intent i = new Intent (MyActivity.this, main.class);

編集 :

//コンテキスト変数を追加

private Context myContext;

public ViewBreakout(Context context){ 
       //your stuff
       this.myContext = context;
}

Intent i = new Intent (myContext, main.class);
于 2013-05-09T18:26:33.190 に答える
0

this の代わりにアクティビティ コンテキストを使用する

      Intent i = new Intent (ActivityName.this, main.class);

ActivityName はアクティビティ クラスの名前です (MainActivity など)。

非アクティビティ コンテキストでインテントを使用している場合は、コンテキストをそのクラスのコンストラクターに渡し、そのコンテキストを使用します。活動開始も同様です。コンテキストを使用して非アクティビティ クラスでアクティビティを開始する context.startActivity(i);

   Context mContext;  
   public ViewBreakout(Context context)
   {
   super(context);
   mContext= context; 
   ....   
   }
   .....
       @Override
    public void onClick(DialogInterface dialog, int which) {
       Intent i = new Intent (mContext, main.class);
       mContext.startActivity(i); 
    }
});
于 2013-05-09T18:23:33.577 に答える