2011-02-03 83 views
0

我有一個繪圖畫布,其中有一個OnTouchListener,我將其稱爲AlertDialog如何從對話框返回無效()?

在對話框中,我重置畫布的底層數據(或不依賴於用戶)。在回到畫布dialog.cancel()畫布不重畫,我希望它。
這意味着用戶必須點擊畫布才能重畫 - 不好!

由於該對話框異步運行,任何在畫布中調用invalidate()的操作都會在對話框返回已更改的基礎數據之前完成。我做任何嘗試無效或從對話框按鈕代碼內引用畫布導致錯誤。我似乎在趕上22!

可以enyone給我建議嗎?

對話框代碼:

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) { 

的Class 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();  
+1

`我做的任何嘗試無效或從對話框按鈕代碼中引用畫布導致錯誤` - 可能有助於向我們顯示此代碼和由此產生的錯誤。 – 2011-02-03 16:38:40

回答

0

兩種可能性:

  • 使對話框不可撤銷
  • 設置OnCancelListener的對話框,將然後調用invalidate()。
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(); 

該錯誤可能會來自你使用無效的情況下,更有關您正在製作的畫布更改的信息以及對話框代碼是必需的。

////////////////////////////////編輯

它看起來像有是之間的問題查看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 

查看myBoard中的onCreate本地初始化,但全球視野myBoard尚未因局部範圍初始化。你有兩個myBoard變量,只有一個被初始化。要快速解決這個問題,我會改變:

View myBoard = new NewBoard(this); 

myBoard = new NewBoard(this); 
+0

你的線「mapView.invalidate();」是我一直在嘗試的代碼。它編譯但運行時出現nullPointerException異常。再次感謝。 – DMW 2011-02-03 20:15:46

+0

那麼,你的錯誤來自myBoard.invalidate();所以我們需要更多的代碼來遍歷它可能爲空的地方。 – ice911 2011-02-03 20:45:36