繁体   English   中英

从SurfaceView开始新活动

[英]Starting a new activity from a surfaceview

尝试从我的类GameView.class开始一个扩展SurfaceView的新活动。 有了这个(在Stackoverflow上找到它):

Context context = GameView.this.getContext();
context.startActivity(new Intent(context, ScoreScreen.class));

我将其放在从单独的线程调用的方法中。 触发后,该应用程序将冻结。 我尝试将其直接放在onTouch事件中(以跳过整个游戏并更快地对其进行测试),并且崩溃的原因包括:

11-27 08:00:00.430: W/dalvikvm(1772): threadid=12: thread exiting with uncaught exception (group=0x41e7b300)
11-27 08:00:00.430: E/AndroidRuntime(1772): FATAL EXCEPTION: Thread-598
11-27 08:00:00.430: E/AndroidRuntime(1772): java.lang.NullPointerException
11-27 08:00:00.430: E/AndroidRuntime(1772):     at com.tricky.puzzlepoker.GameView.onDraw(GameView.java:173)
11-27 08:00:00.430: E/AndroidRuntime(1772):     at com.tricky.puzzlepoker.MainThread.run(MainThread.java:38)
11-27 08:00:02.505: I/Process(1772): Sending signal. PID: 1772 SIG: 9

我不知道该怎么办,需要帮助!

SurfaceView类从View扩展,并且没有onCreate()方法。 但是构造函数需要一个上下文,在创建GameView对象时必须忽略该上下文。 因此,我建议您将上下文存储到全局变量中,以供以后在此类中使用:

   private class GameView extends SurfaceView {
     private Context mContext;
     ...
     public GameView(Context context, AttributeSet attrs, int defStyle){
       super(context, attrs, defStyle);
       this.mContext = context;
       ...
     }
     ...
    }

否则,您可以使用以下方法在GameView类中的其他任何地方获取上下文:

    mContext = getContext();

在这种情况下,您可以稍后开始活动:

    Intent intent = new Intent(mContext, ScoreScreen.class);
    mContext.startActivity(intent);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM