简体   繁体   English

空指针异常

[英]NullPointerException

I released a free app image slider with spoken words for small kids.我发布了一个免费的应用程序图像滑块,其中包含适合小孩子的口语。 Weeks ago, I got the first crash report.几周前,我收到了第一份崩溃报告。 This seems to happen only rarely (once a week).这似乎很少发生(每周一次)。

The report states: NullPointerException in Splash.onCreate()报告指出: Splash.onCreate() 中的 NullPointerException

Stack tace shows this messages: Stack tace 显示此消息:

java.lang.RuntimeException: Unable to start activity 
java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
    at android.app.ActivityThread.access$600(ActivityThread.java:130)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4745)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
    at co.uk.musenuovo.michal.Splash.onCreate(Splash.java:18)
    at android.app.Activity.performCreate(Activity.java:5008)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)

My code in spash activity (intial screen displaying image and playing tune for 5 seconds):我在 spash 活动中的代码(初始屏幕显示图像并播放曲调 5 秒):

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;

public class Splash extends Activity {

    MediaPlayer ourSong;

    @Override
    protected void onCreate(Bundle ShowSplashPage) {
        // TODO Auto-generated method stub
        super.onCreate(ShowSplashPage);
        setContentView(R.layout.splash);
        ourSong = MediaPlayer.create(Splash.this, R.raw.xylophone_open);
        ourSong.start();
        Thread timer = new Thread() {

            public void run() {
                try{
                    sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    Intent openStartingPoint = new Intent("co.uk.musenuovo.michal.STARTINGPOINT");
                    startActivity(openStartingPoint);

                }
            }

        };
        timer.start();

    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        ourSong.release();
        finish();
    }

}

The error clearly points to onstart in spash activity.该错误明确指出了 spash 活动中的 onstart。 Is this because I forgot to import some library?这是因为我忘记导入一些库了吗? or did I forgot to assign something?还是我忘了分配什么? I am very new to java and do not know how to debug and find out what is the problem.我对java很陌生,不知道如何调试并找出问题所在。 I read a lot about NullPointerException but this seems to be so vague of an error.我读了很多关于NullPointerException但这似乎是一个错误的模糊。 Thank you for all your help.谢谢你的帮助。

May be the ourSong variable is null..you need to check before using it可能是 ourSong 变量为空..你需要在使用前检查

ourSong  = MediaPlayer.create(Splash.this, R.raw.xylophone_open);

    if(ourSong  == null) {
        Log.v("LOG", "Create() on MediaPlayer failed.");
    }

您应该始终对来自包的数据进行空检查,包括包本身。

One value is clearly null, it could be the uninitiated variable 'ourSong' or the bundle that you run the method with.一个值显然是 null,它可能是未初始化的变量“ourSong”或您运行该方法的包。 Add a line such as System.out.println(ourSong) and/or another for the bundle, and discover which variable is null.为包添加一行如 System.out.println(ourSong) 和/或另一行,并发现哪个变量为空。

Looks like ourSong is null.看起来ourSong是空的。

Use simple check:使用简单的检查:

if(ourSong != null) {
   ourSong.start();
}

And next time when the ourSong will be null it just won't start() instead of crash your app.下次当ourSongnull它不会start()而不是使您的应用程序崩溃。

Since this is an run time error you won't be probably getting an error while debbugging on your system.由于这是一个运行时错误,您在系统上调试时可能不会遇到错误。 Look for every period (.) or open bracket ([) on the line of code that caused the error.在导致错误的代码行中查找每个句点 (.) 或左括号 ([)。 Something immediately to the left of one of these periods/brackets must have been the null value.紧靠这些句点/括号之一左侧的东西一定是空值。 The error would have been generated by错误是由

ourSong = MediaPlayer.create(Splash.this, R.raw.xylophone_open);
ourSong.start();

I would suggest you to print value of ourSong before starting the service.我建议您在开始服务之前打印 ourSong 的值。

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

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