空指针异常

时间:2012-11-27 12:41:14

标签: android

我发布了一个免费的应用程序图片滑块,为小孩子提供口语。几周前,我收到了第一份崩溃报告。这似乎很少发生(每周一次)。

报告指出:Splash.onCreate()

中的NullPointerException

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)

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

}

该错误明确指出了spash活动中的启动。这是因为我忘了导入一些图书馆吗?还是我忘了分配什么?我是java的新手,不知道如何调试并找出问题所在。我读了很多关于NullPointerException的内容,但这似乎是一个错误的模糊。谢谢你的帮助。

5 个答案:

答案 0 :(得分:3)

可能是mySong变量为null ..你需要在使用之前检查

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

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

答案 1 :(得分:0)

您应该始终检查来自捆绑包的数据,包括捆绑包本身。

答案 2 :(得分:0)

一个值显然为null,它可能是未启动的变量'ourSong'或您运行该方法的包。为bundle添加一行,如System.out.println(ourSong)和/或另一行,并发现哪个变量为null。

答案 3 :(得分:0)

看起来if os.path.isdir(directoryChosen): for n in os.listdir(directoryChosen): self.listWidget.addItem(n) newname = n.replace('$', '#') if newname != n: path = os.path.join(directoryChosen, n) target = os.path.join(directoryChosen, newname) os.rename(path, target) newdir = directoryChosen.replace('$', '#') if directoryChosen != newdir os.rename(directoryChosen, newdir) 为空。

使用简单的检查:

ourSong

下次当if(ourSong != null) { ourSong.start(); } ourSong时,它只会赢得null而不是让您的应用崩溃。

答案 4 :(得分:0)

由于这是一个运行时错误,因此在对系统进行调试时不会出现错误。 在导致错误的代码行上查找每个句点(。)或打开括号([)。紧接其中一个句点/括号左侧的东西必须是空值。 该错误将由

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

我建议你在开始服务之前打印我们的歌曲值。

相关问题