诊断“RuntimeException:无法启动活动,原因是:java.lang.NullPointerException”
我不知道该转向哪里,我正在关注 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的 Orb 类尝试从
null
引用初始化 halfBmpX(和Y
)。初始化应该在构造函数中进行,在您有一个有效的
位图
进行初始化之后。说明:类开头的初始化在构造函数运行之前运行,因此位图除了
null
之外不存在任何其他内容参考。Your
Orb
class attempts to initializehalfBmpX
(andY
) from anull
reference.That initialization should take place in the constructor after you have a valid
Bitmap
to initialize from.Explanation: those initializations at the beginning of the class are run before the constructor runs, so
bitmap
doesn't exist as anything other than anull
reference.您尝试在分配位图之前调用位图上的方法,但它仍然为
null
:您应该将以下两行移到构造函数内:
并将它们的声明保留在构造函数外部:
You try to call a method on bitmap before it was assigned, and it is still
null
:You should move the following two lines inside your constructor:
and keep the declaration of them outside the constructor:
您的
Orb
类中有以下代码:您对
bitmap
对象进行了两次引用,但没有对其进行初始化。您应该在构造函数中设置halfBmpX
和halfBmpY
。You have the following code in your
Orb
class:You've made two references to the
bitmap
object without initializing it. You should sethalfBmpX
andhalfBmpY
in the constructor instead.在
第 13 行 &在 Orb 类的 14
中,当 bitmap 尚未初始化且为 null 时执行此操作,因此会出现空指针异常。
在初始化
bitmap
后,在构造函数内移动右侧的bitmap.getWidth() /2;
位。In
Line 13 & 14
of your Orb class you do thiswhen
bitmap
hasn't been initialized and is null hence the Null Pointer Exception.Move the right
bitmap.getWidth() /2;
bits inside the constructor, afterbitmap
has been initialized.