0

앞에 다른 뷰를 가져 오려고하면 예외가 발생합니다. 내 BoardView 클래스의 마지막 메서드에서 if (lives == 0)라고합니다. 아무도 내가 어디에서 bringToFront 메서드를 호출해야 볼 수 있습니까 ??잘못된 스레드 예외에서 호출 됨

public class BoardView extends SurfaceView implements SurfaceHolder.Callback{ 
Context mContext; 

// thread initialization 
private BoardThread thread; 
Thread timer; 

// box variables 
Bitmap box = 
    (BitmapFactory.decodeResource 
      (getResources(), R.drawable.box)); 
private int box_x = 140; 
private int box_y = 378; 
private int boxWidth = box.getWidth(); 
private int boxHeight = box.getHeight(); 

// storage 
private Vector<Blossom> blossomVector = new Vector<Blossom>(); 
Iterator<Blossom> dataIterator = blossomVector.iterator(); 

// counters 
private int blossomNum = 0; 
private String score; 
private int currentScore = 0; 
private int lives = 3; 

boolean mode = false; 
boolean game = false; 

OutputStreamWriter out = null; 
FileOutputStream fOut = null; 

private static final String TAG = "Debug"; 
final Paint scorePaint = new Paint(); 

public BoardView(Context context){ 
    super(context); 

    scorePaint.setColor(Color.BLACK); 
    scorePaint.setTextSize(12); 
    scorePaint.setTypeface(Typeface.MONOSPACE); 


    //surfaceHolder provides canvas that we draw on 
    getHolder().addCallback(this); 

    // controls drawings 
    thread = new BoardThread(getHolder(),this, blossomVector, dataIterator, box_x, box_y, 
      boxWidth, boxHeight); 

    timer = new Thread(){ 
     public void run(){ 
      //makes sure the player still has 3 lives left 
      while(game == false){ 
       uiCallback.sendEmptyMessage(0); 
       try { 
        Thread.sleep(2000); // wait two seconds before drawing the next flower 
       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } //sleep for 2 seconds 
      } 
     } 
    }; 
    timer.start(); 

    //intercepts touch events 
    setFocusable(true); 

} 

@Override 

public void onDraw(Canvas canvas){ 
    canvas.drawColor(Color.WHITE); 
    score = "SCORE: " + currentScore; 

    //note: pay attention to order you draw things 
    //don't change order or else blossoms will fall 
    //on top of box, not "into" it. 

    //display the scoreboard 
    canvas.drawText(score,240,420,scorePaint); 
    // uses a synchronized method to prevent concurrent modification 
    DrawBlossoms(canvas); 
    canvas.drawBitmap(box, box_x, box_y, null); 

} 

@Override 
public boolean onTouchEvent(MotionEvent event){ 
    //handles movement of box 
    if(event.getAction() == MotionEvent.ACTION_DOWN){ 
     if(event.getX() > box_x & event.getY() > box_y & 
       event.getX() < box_x + boxWidth & event.getY() < box_y + boxHeight) 
     { 
      mode = true; 
     } 
    } 

    if(event.getAction() == MotionEvent.ACTION_MOVE) { 
     if(event.getX() > box_x & event.getY() > box_y & 
       event.getX() < box_x + boxWidth & event.getY() < box_y + boxHeight) 
     { 
      mode = true; 
     } 
     if(mode == true){ 
      box_x = (int)event.getX(); 
     } 

    } 

    if(event.getAction() == MotionEvent.ACTION_UP){ 
     mode = false; 
    } 

    invalidate(); 
    return true; 
} 

@Override 
public void surfaceChanged(SurfaceHolder holder, 
     int format, int width, int height){ 
    Log.v(TAG, "Surface Changed"); 
    //somehow these don't seem to be working 
} 

@Override 
public void surfaceCreated(SurfaceHolder holder){ 
    thread.startRunning(true); 
    thread.start(); 
} 

@Override 
public void surfaceDestroyed(SurfaceHolder holder){ 
    Log.v(TAG, "Surface Destroyed"); 
    //somehow these don't seem to be working 
    thread.startRunning(false); 
    thread.stop(); 
    timer.interrupt(); 
    timer.stop(); 
} 

private Handler uiCallback = new Handler(){ 
    public synchronized void handleMessage(Message msg){ 
     //add a new blossom to the blossom Vector!! 
     blossomVector.add(new Blossom( 
      (BitmapFactory.decodeResource 
        (getResources(), R.drawable.blossom)))); 
     dataIterator = blossomVector.iterator(); 
     blossomNum++; 
     Log.v(TAG, "Number of Blossoms =" + blossomNum); 
    } 
}; 

private synchronized void DrawBlossoms(Canvas c) // method to draw flowers on screen and test for collision 
{ 
    Canvas canvas = c; 
    dataIterator = blossomVector.iterator(); 
    while (dataIterator.hasNext()) 
    { 
     Blossom tempBlossom = dataIterator.next(); 
     tempBlossom.Draw(canvas); 
     if (tempBlossom.hit(box_x,box_y, box_x + boxWidth, box_y + boxHeight, blossomVector) == true) 
     { 
      Log.v(TAG, "ITERATOR WORKS!"); 
      dataIterator.remove(); 
      currentScore += 100; 
     } 

     if (tempBlossom.dropped() == true) 
     { 
      dataIterator.remove(); 
      Log.v(TAG, "Blossom dropped"); 
      lives--; 
     } 
     if (lives == 0) 
     { 
      // END THE GAME!!! 
      ((BoardView) getParent()).findViewById(1).bringToFront(); 
     } 

    } 
} 

public void writeFile() 
{ 
} 

}

public class BoardThread extends Thread { 

private SurfaceHolder surfaceHolder; 
private BoardView boardView; 

private Vector<Blossom> blossomVector; 
private int boxX; 
private int boxY; 
private int boxWidth; 
private int boxHeight; 
private boolean mrun =false; 
private Iterator<Blossom> iterator; 

private static final String TAG = "Debug"; 

public BoardThread(SurfaceHolder holder, BoardView boardView2, 
     Vector<Blossom> blossomVector1, Iterator<Blossom> dataIterator, 
     int box_x, int box_y, int boxW, int boxH) { 

    surfaceHolder = holder; 
    boardView=boardView2; 

    blossomVector = blossomVector1; 
    iterator = dataIterator; 
    boxX = box_x; 
    boxY = box_y; 
    boxW = boxWidth; 
    boxH = boxHeight; 
} 

public void startRunning(boolean run) { 

    mrun=run; 
} 

@Override 
public void run() { 

    super.run(); 
    Canvas canvas; 
    while (mrun) { 
     canvas=null; 
     try { 
      canvas = surfaceHolder.lockCanvas(null); 
       synchronized (surfaceHolder) { 
       //update position 
       //Position(blossomVector, boxX, boxY, boxWidth, boxHeight); 
       // draw flowers 
       boardView.onDraw(canvas); 
      } 
     } finally { 
       if (canvas != null) { 
       surfaceHolder.unlockCanvasAndPost(canvas); 
      } 
     } 
    } 
    } 

private synchronized void Position(Vector<Blossom> blossomVector,int box_x, int box_y, 
     int boxWidth, int boxHeight) 
{ 
    //for(Blossom blossom: blossomVector) 
    iterator = blossomVector.iterator(); 
    while (iterator.hasNext()) 
    { 
     Blossom tempBlossom = iterator.next(); 
     tempBlossom.UpdatePosition(); 

    } 
} 

} 


public class Board extends Activity { 
@Override 
protected void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 

    //use a frame layout so you can also display a dialog box 
    // allows more than one view to be used 
    FrameLayout f1 = new FrameLayout(this); 
    LinearLayout l1 = new LinearLayout(this); 
    EditText edit = new EditText(this); 

    l1.setOrientation(LinearLayout.VERTICAL); 
    l1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
      LayoutParams.WRAP_CONTENT)); 
    edit.setText("Enter your name!"); 
    l1.setId(1); 
    l1.addView(edit); 


    f1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 
      LayoutParams.FILL_PARENT)); 
    f1.addView(l1); 
    f1.addView(new BoardView(this)); 

    setContentView(f1); 

    //setContentView(new BoardView(this)); 
} 

} 당신은 오른쪽 스레드에서 앞으로 가져 오기 실행해야

답변

5

. 시도해보십시오.

final View v = ((BoardView) getParent()).findViewById(1); 
v.post(new Runnable(){ public void run(){ v.bringToFront(); } }); 

확인해보십시오.

+0

동일한 오류가 발생했습니다. 내가 다른 곳에서 전화해야 할까 ?? –

+1

매우 이상 : FrameLayout을 만든 원래 Activity를 추적 한 다음 해당 Activity에서 Handler를 사용하여 'bringToFront'를 수행해야 할 수도 있습니다. – Femi

+0

그게 무슨 뜻입니까? 난 정말 이것이 어떻게 어려운지 이해하지 못한다. (나는 안드로이드 프로그래밍에 대해 처음이다. 그래서 이것은 정말로 나를 괴롭 히고있다.) –

0

UI 스레드 (SurfaceView 스레드 아님)를 사용하여보기를 업데이트해야합니다. SurfaceView 스레드 내에서 일부보기 변경을 원한다면보기 갱신을 수행하는 새로운 RunOnUiThread를 작성해야합니다.

// From within SurfaceView thread 
new Thread() { 
    public void run() { 
     activity.runOnUiThread(new Runnable() {  
      @Override 
      public void run() { 
       // Update view here 
      } 
     }); 
    } 
}.start(); 
관련 문제