Android 什么代码引发空指针异常?
我也是Java和Android开发的新手,在我的Wildfire S上做了一个简单的游戏已经有一段时间了,一切都进行得很顺利,除了每当我遇到空指针异常时,我似乎无法找到它的确切原因 我搜索过的所有论坛都详细说明了空指针异常是什么,或者如何预先克服它们,但这不是我需要的 我在这里面临的问题是,Eclipse似乎吸引我注意的代码部分似乎完全没有问题,我根本没有编辑它。所以我可能在某个地方错改了什么。由于不知道哪个区域已被编辑,我似乎无法找出抛出错误的原因 日志似乎告诉我错误发生在GoalieMenu.java中的onCreate方法上,该方法已经很长时间没有被编辑了。它只是一个使用mainmenu.xml作为布局的活动,包含三个按钮(开始、howToPlay和退出),每个按钮都有自己的xml,它们看起来都很好 我担心这将是一个非常愚蠢和明显的事情,但我不太熟悉我在看什么,所以它是我花了很多时间,我找不到它!!grr 以下是日志:Android 什么代码引发空指针异常?,android,nullpointerexception,Android,Nullpointerexception,我也是Java和Android开发的新手,在我的Wildfire S上做了一个简单的游戏已经有一段时间了,一切都进行得很顺利,除了每当我遇到空指针异常时,我似乎无法找到它的确切原因 我搜索过的所有论坛都详细说明了空指针异常是什么,或者如何预先克服它们,但这不是我需要的 我在这里面临的问题是,Eclipse似乎吸引我注意的代码部分似乎完全没有问题,我根本没有编辑它。所以我可能在某个地方错改了什么。由于不知道哪个区域已被编辑,我似乎无法找出抛出错误的原因 日志似乎告诉我错误发生在GoalieMenu
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();
}
});
}
}
main menu.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>
GoalieMenu.java:34,我想是这个
howToPlayButton.setOnClickListener
howToPlayButton可能为空
在您怀疑的地方调试并使用
try
和catch
。尝试启动一个新活动,如下所示:
startActivity(new Intent(getApplicationContext(),
GameActivity.class));
HowToPlayActivity也一样
我最好的猜测是,您的一条findViewById
语句出现了一些错误-检查R.id是否引用了元素。实际上存在于您的布局xml中。代码中的微小更改,然后尝试以下操作:
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();
}
});
}
}
试试这个…也发布main menu.xml。好了,发布给大家看:)我这样做了:如果(howToPlayButton!=null){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”);}}}哎哟!不管怎样,我得到的结果只有GoalieMenu.java.43。现在,我不知道这是否有任何意义。我讨厌数行,请用有问题的行更新你的问题。这就是重点。我真的不知道通过告诉你第34行(好吧,现在是43行)是什么代码。我们看不到您的行号,但计算行号似乎表明抛出它的是
howToPlayButton。setOnClickListener
,这表明在呼叫时howToPlayButton
为空。re:您对此答案的第一个评论,请在将其设置为findViewById后尝试检查它是否为空
result在main menu.xml文件和r.java中,所有的引用似乎都很好。我用你的替换了我的startActivity行,它抛出了相同的上一个解决方案:删除所有this
关键字。不,这似乎也没什么作用。不过谢谢你的帮助