我也是 Java 和 Android 开发的新手,在我的 Wildfire S 上制作一个简单的游戏已经有一段时间了,一切进展顺利,除了每当我得到一个空指针异常时,我似乎无法找到究竟是什么导致了它。
我搜索过的所有论坛都详细说明了空指针异常是什么或如何先发制人地克服它们,但这不是我需要的。
我在这里面临的问题是 Eclipse 似乎引起我注意的部分代码似乎完全没问题,而我根本没有编辑它。所以我可能在某个地方错误地改变了一些东西。不知道哪个区域已被编辑,我似乎无法找出引发错误的原因。
日志似乎告诉我错误发生在 GoalieMenu.java 中的 onCreate 方法中,该方法已经很长时间没有被编辑了。它只是一个使用 mainmenu.xml 作为其布局的 Activity,其中包含三个按钮(开始、howToPlay 和 Exit),每个按钮都有自己的 XML,它们看起来都很好......
我担心这将是一件非常愚蠢和明显的事情,但我对我正在看的东西不是很熟悉,所以我花了很长时间才找到它!gr
这是日志:
04-04 16:27:57.277: ERROR/AndroidRuntime(2831): FATAL EXCEPTION: main
04-04 16:27:57.277: ERROR/AndroidRuntime(2831): java.lang.RuntimeException: Unable
to start activity
ComponentInfo{com.luk.games.Goalie/com.luk.games.Goalie.GoalieMenu}:
java.lang.NullPointerException
04-04 16:27:57.277: ERROR/AndroidRuntime(2831): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1830)
04-04 16:27:57.277: ERROR/AndroidRuntime(2831): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1851)
04-04 16:27:57.277: ERROR/AndroidRuntime(2831): at
android.app.ActivityThread.access$1500(ActivityThread.java:132)
04-04 16:27:57.277: ERROR/AndroidRuntime(2831): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
04-04 16:27:57.277: ERROR/AndroidRuntime(2831): at
android.os.Handler.dispatchMessage(Handler.java:99)
04-04 16:27:57.277: ERROR/AndroidRuntime(2831): at
android.os.Looper.loop(Looper.java:150)
04-04 16:27:57.277: ERROR/AndroidRuntime(2831): at
android.app.ActivityThread.main(ActivityThread.java:4277)
04-04 16:27:57.277: ERROR/AndroidRuntime(2831): at
java.lang.reflect.Method.invokeNative(Native Method)
04-04 16:27:57.277: ERROR/AndroidRuntime(2831): at
java.lang.reflect.Method.invoke(Method.java:507)
04-04 16:27:57.277: ERROR/AndroidRuntime(2831): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-04 16:27:57.277: ERROR/AndroidRuntime(2831): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-04 16:27:57.277: ERROR/AndroidRuntime(2831): at
dalvik.system.NativeStart.main(Native Method)
04-04 16:27:57.277: ERROR/AndroidRuntime(2831): Caused by:
java.lang.NullPointerException
04-04 16:27:57.277: ERROR/AndroidRuntime(2831): at
com.luk.games.Goalie.GoalieMenu.onCreate(GoalieMenu.java:34)
04-04 16:27:57.277: ERROR/AndroidRuntime(2831): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
04-04 16:27:57.277: ERROR/AndroidRuntime(2831): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1794)
04-04 16:27:57.277: ERROR/AndroidRuntime(2831): ... 11 more
这是 GoalieMenu.java:
package com.luk.games.Goalie;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
public class GoalieMenu extends Activity {
private Button startGameButton;
private Button howToPlayButton;
private Button exitButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.mainmenu);
this.startGameButton = (Button)this.findViewById(R.id.startGameButton);
this.startGameButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
startActivity(new Intent("com.luk.games.Goalie.GameActivity"));
}
});
this.howToPlayButton = (Button)this.findViewById(R.id.howToPlayButton);
this.howToPlayButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
startActivity(new Intent("com.luk.games.Goalie.HowToPlayActivity"));
}
});
this.exitButton = (Button)this.findViewById(R.id.exitButton);
this.exitButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
}
主菜单.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:background="@drawable/mainmenuscreen"
android:layout_height="match_parent" android:orientation="vertical">
<Button
android:id="@+id/startGameButton"
android:background="@layout/startgamebutton"
android:layout_height="50dp"
android:layout_width="180dp"
android:layout_gravity="center"
android:layout_marginTop="100dip"/>
<Button
android:id="@+id/howToPlayButton"
android:background="@layout/howtoplaybutton"
android:layout_height="50dp"
android:layout_width="180dp"
android:layout_gravity="center"/>
<Button
android:id="@+id/exitButton"
android:background="@layout/exitbutton"
android:layout_height="50dp"
android:layout_width="180dp"
android:layout_gravity="center"/>
</LinearLayout>