当我更改 Android 的屏幕方向时,为什么会在画布上出现空指针异常?

发布于 2024-11-30 01:44:50 字数 793 浏览 0 评论 0原文

我的程序在以任何旋转/方向启动时都运行得很好,但是当我在运行时更改横向<->纵向之间的方向时,由于我的画布,我得到一个空指针异常。

@Override
public void run(){
    while(running){
        if(surfaceHolder.getSurface().isValid()){
            Canvas canvas = surfaceHolder.lockCanvas();
            canvas.drawColor(Color.BLACK); //NULLPOINTEREXCEPTION here
            paint(canvas); //another function of mine
            surfaceHolder.unlockCanvasAndPost(canvas);
        }
    }
}

我的清单中有 android:configChanges="orientation" 以及

@Override
public void onConfigurationChanged(Configuration newConfig){
    super.onConfigurationChanged(newConfig);
    setContentView(R.layout.main);
}

当我注释掉 canvas.drawColor(Color.BLACK) 时,调用 Paint(canvas) ,然后在下次使用 canvas 时发生空指针异常功能。

帮助?

My program runs perfectly well when starting in any rotation/orientation, but when I change the orientation between landscape<->portrait while it's running, I get a null pointer exception because of my canvas.

@Override
public void run(){
    while(running){
        if(surfaceHolder.getSurface().isValid()){
            Canvas canvas = surfaceHolder.lockCanvas();
            canvas.drawColor(Color.BLACK); //NULLPOINTEREXCEPTION here
            paint(canvas); //another function of mine
            surfaceHolder.unlockCanvasAndPost(canvas);
        }
    }
}

And I have android:configChanges="orientation" in my manifest as well as

@Override
public void onConfigurationChanged(Configuration newConfig){
    super.onConfigurationChanged(newConfig);
    setContentView(R.layout.main);
}

When I comment out canvas.drawColor(Color.BLACK), paint(canvas) gets called and then the null pointer exception happens the next time canvas is used in that function.

Help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深海蓝天 2024-12-07 01:44:50

SurfaceHolder 的 Javadoc 表示 如果 Canvas 尚未准备好,lockCanvas() 将返回 null,因此您需要为此做好准备。它表示使用 Callback.surfaceCreated() 回调来了解何时可以安全地使用 Canvas

The Javadoc for SurfaceHolder says that lockCanvas() will return null if the Canvas is not ready yet, so you need to be prepared for that. It says to use the Callback.surfaceCreated() callback to know when it's safe to use the Canvas.

放低过去 2024-12-07 01:44:50

我建议看一下这个线程:
Activity 在旋转 Android 上重新启动

具体来说:
创建一个应用程序类并将画布初始化移动到那里。

public class MyApplicationClass extends Application {
  @override
  public void onCreate() {
     super.onCreate();
     // TODO Put your application initialization code here.
  }
}

I would recommend taking a look at this thread:
Activity restart on rotation Android

specifically:
creating an Application class and moving the canvas initialization there.

public class MyApplicationClass extends Application {
  @override
  public void onCreate() {
     super.onCreate();
     // TODO Put your application initialization code here.
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文