强制关闭Java Null异常

时间:2012-02-16 13:42:48

标签: android

我在尝试启动应用时遇到强制关闭错误。它由java.lang.NullPointerException引起。我发布了代码和LogCat,希望有人能搞清楚。

//initialize game and surface class
public class RunfoxrunActivity extends Activity
{
/** Called when the activity is first created. */

private PowerManager.WakeLock wl;
private SurfacePanel game;

//saving state
public static SharedPreferences mPrefs;
public static SharedPreferences.Editor ed;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    mPrefs = getSharedPreferences("com.kobaj.runfoxrun_prefs", 0);
    ed = mPrefs.edit();

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "DoNotDimScreen");

    setVolumeControlStream(AudioManager.STREAM_MUSIC);

    game = new SurfacePanel(this);
    game.onInitialize();

    // last
    setContentView(game);
}

@Override
protected void onPause()
{
    super.onPause();
    wl.release();

    game.onScreenPause(ed);
    ed.commit();
}

@Override
protected void onResume()
{
    super.onResume();
    wl.acquire();

    game.onScreenResume(mPrefs);
}

@Override
protected void onDestroy()
{
    super.onDestroy();

    game.onScreenQuit(ed);
    ed.commit();
}

@Override
public boolean onKeyDown(int i, KeyEvent event)
{
    if (i == KeyEvent.KEYCODE_BACK || i == KeyEvent.KEYCODE_HOME || i == KeyEvent.KEYCODE_SEARCH || i == KeyEvent.KEYCODE_MENU)
        game.onUserPause();

    if (i == KeyEvent.KEYCODE_VOLUME_DOWN || i == KeyEvent.KEYCODE_VOLUME_UP)
        return false;

    game.im.eventUpdateDown(i, event);
    return true;
}

@Override
public boolean onKeyUp(int i, KeyEvent event)
{
    game.im.eventUpdateUp(i, event);
    return true;
}

@Override
public boolean onTouchEvent(MotionEvent e)
{
    game.im.eventUpdate(e);
    return true;
}
}

LogCat是

    02-16 13:39:35.110: I/AudioSystem(20006): getting audio flinger
    02-16 13:39:35.120: I/AudioSystem(20006): returning new audio session id
    02-16 13:39:35.120: D/MediaPlayer(20006): reset() in
    02-16 13:39:35.130: D/MediaPlayer(20006): reset() out
    02-16 13:39:35.130: I/AudioSystem(20006): getting audio flinger
    02-16 13:39:35.130: I/AudioSystem(20006): returning new audio session id
    02-16 13:39:35.240: D/MediaPlayer(20006): start() in
    02-16 13:39:35.360: D/MediaPlayer(20006): start() out
    02-16 13:39:35.711: I/dalvikvm(20006): Could not find method javax.xml.stream.XMLInputFactory.newInstance, referenced from method org.simpleframework.xml.stream.StreamProvider.<init>
    02-16 13:39:35.711: W/dalvikvm(20006): VFY: unable to resolve static method 757: Ljavax/xml/stream/XMLInputFactory;.newInstance ()Ljavax/xml/stream/XMLInputFactory;
    02-16 13:39:35.711: D/dalvikvm(20006): VFY: replacing opcode 0x71 at 0x0003
    02-16 13:39:35.711: D/dalvikvm(20006): VFY: dead code 0x0006-0009 in Lorg/simpleframework/xml/stream/StreamProvider;.<init> ()V
    02-16 13:39:35.711: W/dalvikvm(20006): VFY: unable to find class referenced in signature (Ljavax/xml/stream/XMLEventReader;)
    02-16 13:39:35.711: W/dalvikvm(20006): VFY: unable to find class referenced in signature (Ljavax/xml/stream/XMLEventReader;)
    02-16 13:39:35.711: I/dalvikvm(20006): Could not find method javax.xml.stream.XMLInputFactory.createXMLEventReader, referenced from method org.simpleframework.xml.stream.StreamProvider.provide
    02-16 13:39:35.711: W/dalvikvm(20006): VFY: unable to resolve virtual method 755: Ljavax/xml/stream/XMLInputFactory;.createXMLEventReader (Ljava/io/InputStream;)Ljavax/xml/stream/XMLEventReader;
    02-16 13:39:35.711: D/dalvikvm(20006): VFY: replacing opcode 0x6e at 0x0002
    02-16 13:39:35.711: D/dalvikvm(20006): VFY: dead code 0x0005-000a in Lorg/simpleframework/xml/stream/StreamProvider;.provide (Ljava/io/InputStream;)Lorg/simpleframework/xml/stream/EventReader;
    02-16 13:39:35.711: I/dalvikvm(20006): Could not find method javax.xml.stream.XMLInputFactory.createXMLEventReader, referenced from method org.simpleframework.xml.stream.StreamProvider.provide
    02-16 13:39:35.721: W/dalvikvm(20006): VFY: unable to resolve virtual method 756: Ljavax/xml/stream/XMLInputFactory;.createXMLEventReader (Ljava/io/Reader;)Ljavax/xml/stream/XMLEventReader;
    02-16 13:39:35.721: D/dalvikvm(20006): VFY: replacing opcode 0x6e at 0x0002
    02-16 13:39:35.721: D/dalvikvm(20006): VFY: dead code 0x0005-000a in Lorg/simpleframework/xml/stream/StreamProvider;.provide (Ljava/io/Reader;)Lorg/simpleframework/xml/stream/EventReader;
    02-16 13:39:35.991: D/dalvikvm(20006): GC_CONCURRENT freed 371K, 48% free 2979K/5639K, external 0K/0K, paused 36ms+44ms
    02-16 13:39:36.081: E/JAKOBERR(20006): org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.Attribute(empty=, name=, required=true) on field 'xstartpos' private int com.kobaj.runfoxrun.Animation.xstartpos for class com.kobaj.runfoxrun.Animation at line 9
    02-16 13:39:36.261: D/dalvikvm(20006): GC_CONCURRENT freed 548K, 49% free 2985K/5831K, external 0K/0K, paused 90ms+2ms
    02-16 13:39:36.311: E/JAKOBERR(20006): org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.Attribute(empty=, name=, required=true) on field 'xstartpos' private int com.kobaj.runfoxrun.Animation.xstartpos for class com.kobaj.runfoxrun.Animation at line 9
    02-16 13:39:36.331: D/dalvikvm(20006): GC_EXPLICIT freed 337K, 50% free 2939K/5831K, external 0K/0K, paused 28ms
    02-16 13:39:38.173: E/JAKOBERR(20006): org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.Attribute(empty=, name=, required=true) on field 'xstartpos' private int com.kobaj.runfoxrun.Animation.xstartpos for class com.kobaj.runfoxrun.Animation at line 9
    02-16 13:39:38.183: D/dalvikvm(20006): GC_CONCURRENT freed 421K, 49% free 3027K/5831K, external 0K/0K, paused 4ms+5ms
    02-16 13:39:38.263: E/JAKOBERR(20006): org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.Attribute(empty=, name=, required=true) on field 'xstartpos' private int com.kobaj.runfoxrun.Animation.xstartpos for class com.kobaj.runfoxrun.Animation at line 9
    02-16 13:39:38.323: D/dalvikvm(20006): GC_CONCURRENT freed 616K, 50% free 2980K/5895K, external 0K/0K, paused 3ms+3ms
    02-16 13:39:38.393: E/JAKOBERR(20006): org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.Attribute(empty=, name=, required=true) on field 'xstartpos' private int com.kobaj.runfoxrun.Animation.xstartpos for class com.kobaj.runfoxrun.Animation at line 9
    02-16 13:39:38.463: D/dalvikvm(20006): GC_CONCURRENT freed 494K, 50% free 2997K/5895K, external 0K/0K, paused 2ms+2ms
    02-16 13:39:38.513: E/JAKOBERR(20006): org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.Attribute(empty=, name=, required=true) on field 'xstartpos' private int com.kobaj.runfoxrun.Animation.xstartpos for class com.kobaj.runfoxrun.Animation at line 12
    02-16 13:39:38.594: D/dalvikvm(20006): GC_CONCURRENT freed 513K, 50% free 2995K/5895K, external 0K/0K, paused 4ms+3ms
    02-16 13:39:38.644: E/JAKOBERR(20006): org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.Attribute(empty=, name=, required=true) on field 'xstartpos' private int com.kobaj.runfoxrun.Animation.xstartpos for class com.kobaj.runfoxrun.Animation at line 12
    02-16 13:39:38.734: D/dalvikvm(20006): GC_CONCURRENT freed 500K, 49% free 3007K/5895K, external 0K/0K, paused 2ms+2ms
    02-16 13:39:38.774: E/JAKOBERR(20006): org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.Attribute(empty=, name=, required=true) on field 'xstartpos' private int com.kobaj.runfoxrun.Animation.xstartpos for class com.kobaj.runfoxrun.Animation at line 9
    02-16 13:39:38.894: D/dalvikvm(20006): GC_CONCURRENT freed 508K, 49% free 3009K/5895K, external 0K/0K, paused 2ms+2ms
    02-16 13:39:38.924: E/JAKOBERR(20006): org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.Attribute(empty=, name=, required=true) on field 'xstartpos' private int com.kobaj.runfoxrun.Animation.xstartpos for class com.kobaj.runfoxrun.Animation at line 12
    02-16 13:39:38.944: D/dalvikvm(20006): GC_EXPLICIT freed 240K, 51% free 2944K/5895K, external 0K/0K, paused 27ms
    02-16 13:39:38.964: D/AndroidRuntime(20006): Shutting down VM
    02-16 13:39:38.964: W/dalvikvm(20006): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0)
    02-16 13:39:38.994: E/AndroidRuntime(20006): FATAL EXCEPTION: main
    02-16 13:39:38.994: E/AndroidRuntime(20006): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kobaj.runfoxrun/com.kobaj.runfoxrun.RunfoxrunActivity}: java.lang.NullPointerException
    02-16 13:39:38.994: E/AndroidRuntime(20006):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1872)
    02-16 13:39:38.994: E/AndroidRuntime(20006):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893)
    02-16 13:39:38.994: E/AndroidRuntime(20006):    at android.app.ActivityThread.access$1500(ActivityThread.java:135)
    02-16 13:39:38.994: E/AndroidRuntime(20006):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054)
    02-16 13:39:38.994: E/AndroidRuntime(20006):    at android.os.Handler.dispatchMessage(Handler.java:99)
    02-16 13:39:38.994: E/AndroidRuntime(20006):    at android.os.Looper.loop(Looper.java:150)
    02-16 13:39:38.994: E/AndroidRuntime(20006):    at android.app.ActivityThread.main(ActivityThread.java:4385)
    02-16 13:39:38.994: E/AndroidRuntime(20006):    at java.lang.reflect.Method.invokeNative(Native Method)
    02-16 13:39:38.994: E/AndroidRuntime(20006):    at java.lang.reflect.Method.invoke(Method.java:507)
    02-16 13:39:38.994: E/AndroidRuntime(20006):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
    02-16 13:39:38.994: E/AndroidRuntime(20006):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
    02-16 13:39:38.994: E/AndroidRuntime(20006):    at dalvik.system.NativeStart.main(Native Method)
    02-16 13:39:38.994: E/AndroidRuntime(20006): Caused by: java.lang.NullPointerException
    02-16 13:39:38.994: E/AndroidRuntime(20006):    at com.kobaj.runfoxrun.SurfacePanel.onInitialize(SurfacePanel.java:124)
    02-16 13:39:38.994: E/AndroidRuntime(20006):    at com.kobaj.runfoxrun.RunfoxrunActivity.onCreate(RunfoxrunActivity.java:52)
    02-16 13:39:38.994: E/AndroidRuntime(20006):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
    02-16 13:39:38.994: E/AndroidRuntime(20006):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836)
    02-16 13:39:38.994: E/AndroidRuntime(20006):    ... 11 more
    02-16 13:39:40.976: D/Process(20006): killProcess, pid=20006
    02-16 13:39:40.986: D/Process(20006): dalvik.system.VMStack.getThreadStackTrace(Native Method)
    02-16 13:39:40.986: D/Process(20006): java.lang.Thread.getStackTrace(Thread.java:745)
    02-16 13:39:40.986: D/Process(20006): android.os.Process.killProcess(Process.java:797)
    02-16 13:39:40.986: D/Process(20006): com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:108)
    02-16 13:39:40.986: D/Process(20006): java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:854)
    02-16 13:39:40.986: D/Process(20006): java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:851)
    02-16 13:39:40.986: D/Process(20006): dalvik.system.NativeStart.main(Native Method)
    02-16 13:39:40.986: I/Process(20006): Sending signal. PID: 20006 SIG: 9

2 个答案:

答案 0 :(得分:1)

看起来解析时出现问题

loadingStar = XMLHandler.readSerialFile(getResources(), R.raw.star, Sprite.class);

因此loadingStar为空。从日志中的警告我猜测XML解析器有问题

答案 1 :(得分:0)

你的NPE发生在SurfacePanel第124行。去那里查看。

com.kobaj.runfoxrun.SurfacePanel.onInitialize(SurfacePanel.java:124)