0

我有一个绘图画布,OnTouchListener其中我称之为AlertDialog.

在对话框中,我重置了画布的基础数据(或不取决于用户)。在使用画布返回画布时dialog.cancel()不会重绘,我想要它。
这意味着用户必须单击画布才能使其重绘——不好!

invalidate()因为对话框异步运行,所以在对话框返回更改的基础数据之前,画布中的任何调用都已完成。我从对话框按钮代码中使画布无效或引用画布的任何尝试都会导致错误。我似乎在第 22 阶段!

谁能给我建议?

对话代码:

case DIALOG_NEW_ID:
    AlertDialog.Builder builder5 = new AlertDialog.Builder(this);
    builder5.setMessage("     WARNING" +
            "\nYour current game will be lost")
    .setCancelable(false)
    .setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {                   
            dialog.cancel();
        }
    })              
    .setNegativeButton("Continue", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            cells.reset();
            gameMode = 0;
            dialog.cancel();
        }
    });
    AlertDialog alert5 = builder5.create();             
    alert5.setOnDismissListener(new DialogInterface.OnDismissListener() { 
        public void onDismiss( DialogInterface dialog) { 
            Log.d(TAG, "Dialog cancelled");// debug log entry 
            //myBoard.invalidate();
        }
    });     
    dialog = alert5;                
    break;

onTouch画布中的对话框调用代码:

    case MODE_SOLUTION:
        break;
    default:                            
        showDialog(DIALOG_NEW_ID);
        invalidate();
}// end switch  

主类设置:

public class SudokuGame extends Activity {
static final int DIALOG_OPEN_ID = 0;
static final int DIALOG_INFO_ID = 1;
static final int DIALOG_FAIL_ID = 2;
static final int DIALOG_SAVE_ID = 3;
static final int DIALOG_NEW_ID = 4;
static final int MODE_ENTRY = 0;
static final int MODE_PLAY = 1;
static final int MODE_SHOW_ERRORS = 3;
static final int MODE_SOLUTION = 4;
final String TAG = "Game";
int  gameMode;
SKU cells;
View myBoard;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_LEFT_ICON);
    View myBoard = new NewBoard(this);        
    setContentView(myBoard);
    setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_launcher);
    showDialog(DIALOG_OPEN_ID);        
}// end onCreate

    protected Dialog onCreateDialog(int id) {

嵌套在主类中的类 NewBoard:

    class NewBoard extends View implements OnTouchListener{
    int cW;  // canvas width
    int cH;  // canvas height
    int cellSize,textSize;
    int boardX,boardY;  
    int runner, tX, tY,pX,pY,selectorX,selectorY;
    int gap ;
    int btn1X,btn1Y, selected;
    int infoButtonX, infoButtonY,quitButtonX,quitButtonY;
    boolean btn1Pressed, btn2Pressed,  btn3Pressed, btn4Pressed, btn5Pressed;
    String btn1,btn2,btn3,btn4,btn5;

    public NewBoard(Context context){ // NewBoard Constructor
        super(context);
        this.setOnTouchListener(this);  
        cells = new SKU();
        readStateStore();
    }// end NewBoard Constructor

    @Override 
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();  
        Rect textRect = new Rect();     
4

2 回答 2

2

您能否使用对话框和 invalidate() 发布一些您的情况的代码,以及返回的错误消息?

我不明白为什么您不能执行以下操作(使 mapView 无效的对话框):

    AlertDialog.Builder builderAccountInfo = new AlertDialog.Builder(this);
        builderAccountInfo.setMessage("TEST")
        .setCancelable(false)
        .setTitle("TEST")
        .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
            changeUserCanvas();
            mapView.invalidate();
            dialog.cancel();
       }
   });
        AlertDialog alertAccountInfo = builderAccountInfo.create();

        alertAccountInfo.show();

该错误可能来自您用于使无效的上下文,但需要有关您正在对画布所做的更改和对话框代码的更多信息。

////////////////////////////////编辑

View myBoard 的局部变量和全局变量之间似乎存在问题。

public class SudokuGame extends Activity {
static final int DIALOG_OPEN_ID = 0;
static final int DIALOG_INFO_ID = 1;
static final int DIALOG_FAIL_ID = 2;
static final int DIALOG_SAVE_ID = 3;
static final int DIALOG_NEW_ID = 4;
static final int MODE_ENTRY = 0;
static final int MODE_PLAY = 1;
static final int MODE_SHOW_ERRORS = 3;
static final int MODE_SOLUTION = 4;
final String TAG = "Game";
int  gameMode;
SKU cells;
View myBoard;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_LEFT_ICON);
    View myBoard = new NewBoard(this);        
    setContentView(myBoard);
    setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_launcher);
    showDialog(DIALOG_OPEN_ID);        
}// end onCreate

onCreate 中的 View myBoard 是在本地初始化的,但是全局的 View myBoard 因为本地作用域而没有被初始化。你有两个 myBoard 变量,只有一个被初始化。为了快速解决这个问题,我会改变:

View myBoard = new NewBoard(this);

myBoard = new NewBoard(this); 
于 2011-02-03T16:32:27.837 回答
0

两种可能:

  • 使对话框不可取消
  • 为将调用 invalidate() 的对话框设置 OnCancelListener。
于 2011-02-03T18:31:22.383 回答