诊断“RuntimeException:无法启动活动,原因是:java.lang.NullPointerException”

发布于 2024-12-18 01:23:23 字数 2450 浏览 0 评论 0原文

我不知道该转向哪里,我正在关注 Android 教程 这里 在最新的更新中,我遇到了运行时错误,我真的很难找到它的来源!

我的目标是在屏幕左侧出现一个小“球体”图标,玩家可以将其拖放到表面上。

自从我上次成功编译该程序以来,现在我只对该程序做了一些更改。所有这些都在 GameView 类中,您可以在这里看到:http://pastebin.com/zsYKFmuP。我还创建了一个名为 Orb 的全新类,位于: http://pastebin.com/wQKqcVtV

如果您有android开发经验,然后查看我的代码应该不会太麻烦,但是我感谢任何花时间提出建议的人!所有建议将不胜感激!

logcat 有这样的说法:

 FATAL EXCEPTION: main
 java.lang.RuntimeException: Unable to start activity ComponentInfo{biz.hireholly.tutorial/biz.hireholly.tutorial.MainActivity}: java.lang.NullPointerException
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
     at android.app.ActivityThread.access$1500(ActivityThread.java:121)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
     at android.os.Handler.dispatchMessage(Handler.java:99)
     at android.os.Looper.loop(Looper.java:130)
     at android.app.ActivityThread.main(ActivityThread.java:3701)
     at java.lang.reflect.Method.invokeNative(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:507)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
     at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
     at biz.hireholly.tutorial.models.Orb.<init>(Orb.java:13)
     at biz.hireholly.tutorial.GameView.<init>(GameView.java:34)
     at biz.hireholly.tutorial.MainActivity.onCreate(MainActivity.java:25)
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
     ... 11 more

来自 Orb 类的相关源代码:

public class Orb {

        private Bitmap bitmap;  //image
        private int x;  //x coord
        private int y;  //y coord
        private boolean touched; //if orb is touched/picked up
        //just to make things simpler to read
        int halfBmpX = bitmap.getWidth() /2; // <<<=== Line 13
        int halfBmpY = bitmap.getHeight() /2;

I don't know where to turn, i was following an android tutorial here and upon the latest update i'm getting a runtime error and i'm really struggling to find the source of it!

What im aiming for is a small "orb" icon to appear on the screen in the lop left, that the player can drag and drop across the surface.

Now i've only made a few changes to the program since i last compiled it successfully. All of those were in the GameView class which you can see here: http://pastebin.com/zsYKFmuP. And i also created a brand new class called Orb which is here: http://pastebin.com/wQKqcVtV

If you have experience with android development then looking over my code shouldn't be too troublesome however i thank anyone who takes the time to make any suggestion! All advice will be appreciated!

The logcat has this to say:

 FATAL EXCEPTION: main
 java.lang.RuntimeException: Unable to start activity ComponentInfo{biz.hireholly.tutorial/biz.hireholly.tutorial.MainActivity}: java.lang.NullPointerException
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
     at android.app.ActivityThread.access$1500(ActivityThread.java:121)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
     at android.os.Handler.dispatchMessage(Handler.java:99)
     at android.os.Looper.loop(Looper.java:130)
     at android.app.ActivityThread.main(ActivityThread.java:3701)
     at java.lang.reflect.Method.invokeNative(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:507)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
     at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
     at biz.hireholly.tutorial.models.Orb.<init>(Orb.java:13)
     at biz.hireholly.tutorial.GameView.<init>(GameView.java:34)
     at biz.hireholly.tutorial.MainActivity.onCreate(MainActivity.java:25)
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
     ... 11 more

The relevant source from the Orb class:

public class Orb {

        private Bitmap bitmap;  //image
        private int x;  //x coord
        private int y;  //y coord
        private boolean touched; //if orb is touched/picked up
        //just to make things simpler to read
        int halfBmpX = bitmap.getWidth() /2; // <<<=== Line 13
        int halfBmpY = bitmap.getHeight() /2;

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

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

发布评论

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

评论(4

汐鸠 2024-12-25 01:23:23

您的 Orb 类尝试从 null 引用初始化 halfBmpX(和 Y)。

初始化应该在构造函数中进行,您有一个有效的位图进行初始化之后。

public Orb(Bitmap bmp, int x, int y) {
    this.bitmap = bmp;
    this.x = x;
    this.y = y;
    halfBmpX = bmp.getWidth() / 2;
    halfBmpY = bmp.getHeight() / 2;
}

说明:类开头的初始化在构造函数运行之前运行,因此位图除了 null 之外不存在任何其他内容参考。

Your Orb class attempts to initialize halfBmpX (and Y) from a null reference.

That initialization should take place in the constructor after you have a valid Bitmap to initialize from.

public Orb(Bitmap bmp, int x, int y) {
    this.bitmap = bmp;
    this.x = x;
    this.y = y;
    halfBmpX = bmp.getWidth() / 2;
    halfBmpY = bmp.getHeight() / 2;
}

Explanation: those initializations at the beginning of the class are run before the constructor runs, so bitmap doesn't exist as anything other than a null reference.

陪我终i 2024-12-25 01:23:23

您尝试在分配位图之前调用位图上的方法,但它仍然为 null

int halfBmpX = bitmap.getWidth() /2;

您应该将以下两行移到构造函数内:

halfBmpX = bitmap.getWidth() /2;
halfBmpY = bitmap.getHeight() /2;

并将它们的声明保留在构造函数外部:

int halfBmpX, halfBmpY;

You try to call a method on bitmap before it was assigned, and it is still null:

int halfBmpX = bitmap.getWidth() /2;

You should move the following two lines inside your constructor:

halfBmpX = bitmap.getWidth() /2;
halfBmpY = bitmap.getHeight() /2;

and keep the declaration of them outside the constructor:

int halfBmpX, halfBmpY;
南烟 2024-12-25 01:23:23

您的 Orb 类中有以下代码:

private Bitmap bitmap;  //image
private int x;  //x coord
private int y;  //y coord
private boolean touched; //if orb is touched/picked up
//just to make things simpler to read
int halfBmpX = bitmap.getWidth() /2;
int halfBmpY = bitmap.getHeight() /2;

您对 bitmap 对象进行了两次引用,但没有对其进行初始化。您应该在构造函数中设置 halfBmpXhalfBmpY

You have the following code in your Orb class:

private Bitmap bitmap;  //image
private int x;  //x coord
private int y;  //y coord
private boolean touched; //if orb is touched/picked up
//just to make things simpler to read
int halfBmpX = bitmap.getWidth() /2;
int halfBmpY = bitmap.getHeight() /2;

You've made two references to the bitmap object without initializing it. You should set halfBmpX and halfBmpY in the constructor instead.

硬不硬你别怂 2024-12-25 01:23:23

第 13 行 &在 Orb 类的 14 中,

int halfBmpX = bitmap.getWidth() /2;
int halfBmpY = bitmap.getHeight() /2;

当 bitmap 尚未初始化且为 null 时执行此操作,因此会出现空指针异常。

在初始化 bitmap 后,在构造函数内移动右侧的 bitmap.getWidth() /2; 位。

In Line 13 & 14 of your Orb class you do this

int halfBmpX = bitmap.getWidth() /2;
int halfBmpY = bitmap.getHeight() /2;

when bitmap hasn't been initialized and is null hence the Null Pointer Exception.

Move the right bitmap.getWidth() /2; bits inside the constructor, after bitmap has been initialized.

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