0

我在一个名为“ViewBreakout”的类中有一个警报对话框设置我的按钮设置很好,它会创建按钮,但是当我尝试添加一个意图时,我收到一条错误消息,内容为 “构造函数 Intent(new DialogInterface.OnClickListener() {}, Class) 未定义”。它提供的解决方案是删除参数以匹配意图();???

    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。我真的被卡住了,这是一个即将到期的uni项目。

这是我在课堂上的代码。我想我会带你全班看看有没有我遗漏的东西

    /**
    * 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

您的意图是在另一个类内部类中创建的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

使用活动上下文而不是这个

      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 回答