我也是Java和Android开发的新手,已经在我的Wildfire S上做了一段简单的游戏了一段时间并且事情进展顺利,除非我得到一个空指针异常,我似乎无法找到究竟是什么导致它。
我搜索过的所有论坛,详细说明了Null Pointer Exception是什么,或者如何先发制人地克服它们,但这不是我需要的。
我在这里遇到的问题是,Eclipse似乎引起我注意的代码部分似乎完全正常,我根本没有编辑它。所以我可能在某处错误地改变了一些东西。不知道哪个区域已被编辑,我似乎无法找出错误被抛出的原因。
日志似乎告诉我,在GoalieMenu.java中的onCreate方法中发生了错误,该方法在很长一段时间内没有被编辑过。它只是一个使用mainmenu.xml作为其布局的Activity,它包含三个按钮(start,howToPlay和Exit),每个按钮都有自己的XML,它们看起来都很好......
我担心这将是一个非常愚蠢和显而易见的事情,但我对我所看到的东西并不是很熟悉,所以它花了我很多年,我找不到它! GRR
这是日志:
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();
}
});
}
}
mainmenu.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>
答案 0 :(得分:2)
GoalieMenu.java:34,我认为就是这个howToPlayButton.setOnClickListener
howToPlayButton可能为null。
调试并使用您怀疑的try
和catch
。
答案 1 :(得分:0)
尝试按如下方式开始新活动:
startActivity(new Intent(getApplicationContext(),
GameActivity.class));
HowToPlayActivity
答案 2 :(得分:0)
我最好的猜测是你的一个findViewById
语句出了问题 - 检查R.id引用的元素是否有问题。实际上存在于你的布局xml中。
答案 3 :(得分:0)
您的代码稍有变化,请尝试以下::::
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);
findViewById(R.id.startGameButton).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
startActivity(new Intent("com.luk.games.Goalie.GameActivity"));
}
});
findViewById(R.id.howToPlayButton).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
startActivity(new Intent("com.luk.games.Goalie.HowToPlayActivity"));
}
});
findViewById(R.id.exitButton).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
}
试试这个......