Android 中的线程问题

发布于 2024-09-06 20:46:38 字数 1650 浏览 3 评论 0原文

我正在为android平台制作一个简单的2d游戏,它在2.0及以上版本中完美运行,但在1.6设备上测试时,它立即崩溃。在运行调试器时,我似乎在线程类中遇到了空指针异常。我只是想知道是否有人对问题可能出在哪里有任何想法。

这是线程类的代码:

package com.marcusortiz.burnination;

import android.graphics.Canvas;
import android.view.SurfaceHolder;

public class GameThread extends Thread
{
  private SurfaceHolder sHolder;
  private MainView panel;
  private boolean isRunning;

  public GameThread(SurfaceHolder sHolder, MainView panel)
  {
    this.sHolder = sHolder;
    this.panel = panel;
  }

  public void setRunning(boolean isRunning)
  {
    this.isRunning = isRunning;
  }

  public SurfaceHolder getSurfaceHolder()
  {
    return sHolder;
  }

  @Override
  public void run()
  {
    Canvas canvas;
    while(isRunning)
    {
      canvas = null;
      try
      {
        canvas = sHolder.lockCanvas(null);
        synchronized(sHolder)
        {
          for(Sprite s : panel.getSprites())
          {
            s.update();
          }
          panel.onDraw(canvas);
        }
      }
      finally
      {
        if(canvas != null)
        {
          sHolder.unlockCanvasAndPost(canvas);
        }
      }
    }
  }
}

也许这段代码存在一些我没有意识到的兼容性问题——在使用线程方面我是一个初学者。

编辑:这是请求的堆栈跟踪

burnination [Android Application]   
  DalvikVM[localhost:8608]  
    Thread [<3> main] (Running) 
    Thread [<15> Binder Thread #3] (Running)    
    Thread [<13> Binder Thread #2] (Running)    
    Thread [<11> Binder Thread #1] (Running)    
    Thread [<17> Thread-9] (Suspended (exception NullPointerException)) 
      GameThread.run() line: 53 

I'm making a simple 2d game for the android platform, which works perfectly from version 2.0 and above, but when testing it on a 1.6 device, it crashes immediately. On running the debugger, it seems that I'm getting a null pointer exception in the thread class. I was just wondering if anybody has any ideas as to where the problem might be.

Here's the code for the thread class:

package com.marcusortiz.burnination;

import android.graphics.Canvas;
import android.view.SurfaceHolder;

public class GameThread extends Thread
{
  private SurfaceHolder sHolder;
  private MainView panel;
  private boolean isRunning;

  public GameThread(SurfaceHolder sHolder, MainView panel)
  {
    this.sHolder = sHolder;
    this.panel = panel;
  }

  public void setRunning(boolean isRunning)
  {
    this.isRunning = isRunning;
  }

  public SurfaceHolder getSurfaceHolder()
  {
    return sHolder;
  }

  @Override
  public void run()
  {
    Canvas canvas;
    while(isRunning)
    {
      canvas = null;
      try
      {
        canvas = sHolder.lockCanvas(null);
        synchronized(sHolder)
        {
          for(Sprite s : panel.getSprites())
          {
            s.update();
          }
          panel.onDraw(canvas);
        }
      }
      finally
      {
        if(canvas != null)
        {
          sHolder.unlockCanvasAndPost(canvas);
        }
      }
    }
  }
}

Maybe there's some compatibility issues with this code that I'm not aware of--I'm very much a beginner when it comes to using threads.

EDIT: here's the requested stack trace

burnination [Android Application]   
  DalvikVM[localhost:8608]  
    Thread [<3> main] (Running) 
    Thread [<15> Binder Thread #3] (Running)    
    Thread [<13> Binder Thread #2] (Running)    
    Thread [<11> Binder Thread #1] (Running)    
    Thread [<17> Thread-9] (Suspended (exception NullPointerException)) 
      GameThread.run() line: 53 

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

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

发布评论

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

评论(3

终陌 2024-09-13 20:46:38

这不是堆栈跟踪,但看起来问题出在第 53 行,这正是我想要解决的问题。看起来您发布的代码可能与您正在运行的代码不完全一样,因为第 53 行是右大括号。该行上可能有一个变量为空。

That's not a stack trace, but it looks like the problem is on line 53, which is what I was trying to get at. It looks like the code you posted probably doesn't like up exactly with what you're running, because line 53 is a closing brace. There's probably a variable on that line that's null.

温柔少女心 2024-09-13 20:46:38

我给你一些建议。
1. 找出崩溃发生在哪一行。
2. 检查SDK(1.6 & 2.0)源代码中API的差异。
如果您可以在 Android 2.0 上工作,那么我认为您的代码没有问题。

I give you some suggestions.
1. Find out which line the crash locate.
2. Check the difference of the APIs in the SDK(1.6 & 2.0) source code.
If you can work on Android 2.0, then I don't think there is some problem on your code.

烟柳画桥 2024-09-13 20:46:38

默认情况下,大多数调试器(包括 Eclipse)仅在“未捕获”异常时中断。它们不会在“捕获”异常(即适用“catch”块的任何情况)时中断。您的“finally”块在这里充当包罗万象的角色,因为它包含即使抛出异常也必须执行的代码。

当“try”块中的函数抛出 NullPointerException 时,调试器不会停止,因为它是您可能试图忽略的“捕获”异常。执行“finally”块中的代码,然后重新抛出异常。这次没有人抓住它,所以 Eclipse 停止了。您需要指向“finally”块的右大括号,因为那是重新抛出发生的地方。

如果为 catch+uncaught NullPointerException 配置调试断点,您将在抛出点看到失败。另外,如果您只是让它继续运行并让线程终止,您应该会看到 Android 默认未捕获异常处理程序的 logcat 输出,指示抛出点。

By default, most debuggers (including Eclipse) break only on "uncaught" exceptions. They do not break on "caught" exceptions, i.e. anything for which a "catch" block applies. Your "finally" block is acting as a catch-all here, since it includes code that must be executed even if an exception was thrown.

When a NullPointerException is thrown by a function in the "try" block, the debugger does not stop, because it's a "caught" exception that you might be trying to ignore. The code in the "finally" block executes, and then the exception is re-thrown. This time there's nobody to catch it, so Eclipse halts. You're left pointing at the closing brace of the "finally" block because that's where the re-throw happens.

If you configure a debug breakpoint for caught+uncaught NullPointerException, you will see the failure at the point of the throw. Also, if you just let it continue and let the thread die, you should see logcat output from the Android default uncaught exception handler indicating the point of the throw.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文