我設置了一個程序,其中一個活動(一個菜單)調用另一個菜單。然後這個菜單可以調用各自啓動不同GamePanel /線程的各種活動。在返回到菜單後按下箭頭返回按鈕,但是它們已經崩潰並且不能再使用。在surfaceview/thread崩潰後返回活動:s
第二點,在代碼的第一部分,我不能在偵聽器中設置意圖,因爲eclipse要求Intent參數是空的,因此我創建了按下的方法,任何解釋/解決方案?在此先感謝所有人!
從菜單中調用:
private OnClickListener L1Listen = new OnClickListener(){
public void onClick(View arg0) {
L1Pressed();
}
};
public void L1Pressed()
{
Intent intent = new Intent(this, L1Started.class);
startActivity(intent);
}
從菜單中調用:
package SortItOut.sortitout;
import android.app.Activity;
import android.os.Bundle;
public class L1Started extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MainGamePanelL1((this)));
}
protected void onDestroy()
{
super.onDestroy();
}
protected void onStop()
{
super.onStop();
}
}
面板的構造函數(實現SurfaceHolder.callback)
public MainGamePanelL1(Context context){
super(context);
getHolder().addCallback(this);
thread = new MainThreadL1(getHolder(), this);
setFocusable(true);
主題
package SortItOut.sortitout;
import android.graphics.Canvas;
import android.view.SurfaceHolder;
public class MainThreadL1 extends Thread {
private boolean running;
private SurfaceHolder surfaceholder;
private MainGamePanelL1 gamepanel;
int y;
public void setRunning(boolean running)
{
this.running = running;
}
public MainThreadL1(SurfaceHolder surfaceholder, MainGamePanelL1 mainGamePanelL1)
{
this.surfaceholder = surfaceholder;
this.gamepanel = mainGamePanelL1;
}
public void run()
{
Canvas canvas;
while(running)
{
canvas = null;
try{
canvas = this.surfaceholder.lockCanvas();
synchronized(surfaceholder)
{
gamepanel.Check();
this.gamepanel.onDraw(canvas);
}
}finally
{
if(canvas != null)
{
surfaceholder.unlockCanvasAndPost(canvas);
}
}
}
}
}
那麼,只要將onDraw等移動到線程中就更容易了? –
必須從主線程調用所有與UI相關的活動。 – mportuesisf
這是實際的代碼還是呼叫?什麼是與UI相關的活動? –