空指针异常 [英] NullPointerException

查看:194
本文介绍了空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发布了一个免费的应用程序的图像滑块与小的孩子说出的话。星期前,我得到了第一崩溃报告。这似乎很少(每周一次)的情况发生。

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).

报告称:NullPointerException异常在Splash.onCreate()

The report states: NullPointerException in Splash.onCreate()

堆栈TACE显示了这个消息:

Stack tace shows this messages:

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)

我的code在spash活动(intial屏幕显示图像和播放调整为5秒):

My code in spash activity (intial screen displaying image and playing tune for 5 seconds):

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();
    }

}

错误明确指出在spash活动ONSTART。这是因为我忘了导入某些库?还是我忘了分配的东西?我很新的Java和不知道如何调试,并找出是什么问题。我读了很多关于 NullPointerException异常但这似乎是如此含糊不清的错误。感谢您的所有帮助。

The error clearly points to onstart in spash activity. 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. I read a lot about NullPointerException but this seems to be so vague of an error. Thank you for all your help.

推荐答案

可能是ourSong变量是null..you使用前需要先检查

May be the ourSong variable is null..you need to check before using it

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

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

这篇关于空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆