我设置了一个程序,其中一个活动(一个菜单)调用另一个菜单。然后这个菜单可以调用各自启动不同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相关的活动? –