【发布时间】:2014-08-20 19:51:07
【问题描述】:
您好,希望有人可以帮助我解决我在运行代码时遇到的错误。想要的是当用户从主类获得游戏结束分数时被带到游戏结束类以显示最终分数。没有意图,游戏结束屏幕加载正常,但是当我在游戏结束时添加意图运行游戏时,应用程序崩溃这是我的代码,其中分数是 int:
主类意图:
Intent myIntent = new Intent(MainActivity.this,gameover.class);
myIntent.putExtra("number", score);
MainActivity.this.startActivity(myIntent);
游戏结束类:
EndScore = (TextView) findViewById(R.id.show);
int numScore;
numScore = getIntent().getExtras().getInt("number");
String s = String.valueOf( numScore );
EndScore.setText(s);
我一直在浏览论坛,但看不出我做错了什么。 日志如下:
12-28 02:36:37.760: I/Adreno200-EGLSUB(17096): <ConfigWindowMatch:2081>: Format RGBA_8888.
12-28 02:36:37.770: D/memalloc(17096): /dev/pmem: Mapped buffer base:0x5189a000 size:4866048 offset:4251648 fd:68
12-28 02:36:37.860: D/memalloc(17096): /dev/pmem: Mapped buffer base:0x51e6e000 size:2949120 offset:2334720 fd:71
12-28 02:36:38.870: W/dalvikvm(17096): threadid=1: thread exiting with uncaught exception (group=0x40b0a9f0)
12-28 02:36:38.880: E/AndroidRuntime(17096): FATAL EXCEPTION: main
12-28 02:36:38.880: E/AndroidRuntime(17096): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.phil3992.colourguess/com.phil3992.colourguess.gameover}: java.lang.NullPointerException
12-28 02:36:38.880: E/AndroidRuntime(17096): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
12-28 02:36:38.880: E/AndroidRuntime(17096): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
12-28 02:36:38.880: E/AndroidRuntime(17096): at android.app.ActivityThread.access$600(ActivityThread.java:123)
12-28 02:36:38.880: E/AndroidRuntime(17096): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
12-28 02:36:38.880: E/AndroidRuntime(17096): at android.os.Handler.dispatchMessage(Handler.java:99)
12-28 02:36:38.880: E/AndroidRuntime(17096): at android.os.Looper.loop(Looper.java:137)
12-28 02:36:38.880: E/AndroidRuntime(17096): at android.app.ActivityThread.main(ActivityThread.java:4424)
12-28 02:36:38.880: E/AndroidRuntime(17096): at java.lang.reflect.Method.invokeNative(Native Method)
12-28 02:36:38.880: E/AndroidRuntime(17096): at java.lang.reflect.Method.invoke(Method.java:511)
12-28 02:36:38.880: E/AndroidRuntime(17096): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
12-28 02:36:38.880: E/AndroidRuntime(17096): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
12-28 02:36:38.880: E/AndroidRuntime(17096): at dalvik.system.NativeStart.main(Native Method)
12-28 02:36:38.880: E/AndroidRuntime(17096): Caused by: java.lang.NullPointerException
12-28 02:36:38.880: E/AndroidRuntime(17096): at com.phil3992.colourguess.gameover.onCreate(gameover.java:22)
12-28 02:36:38.880: E/AndroidRuntime(17096): at android.app.Activity.performCreate(Activity.java:4470)
12-28 02:36:38.880: E/AndroidRuntime(17096): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-28 02:36:38.880: E/AndroidRuntime(17096): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
12-28 02:36:38.880: E/AndroidRuntime(17096): ... 11 more
当用户得分时 int 是递增的,然后在游戏结束时它应该发送并显示在 textview 中
更新整个游戏结束类:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class gameover extends Activity {
Button re_run;
TextView EndScore;
@Override
public void onCreate(Bundle savedInstanceState) {
re_run = (Button) findViewById(R.id.retry);
super.onCreate(savedInstanceState);
setContentView(R.layout.gameover);
EndScore = (TextView) findViewById(R.id.show);
int numScore;
numScore = getIntent().getExtras().getInt("number");
String s = String.valueOf( numScore );
EndScore.setText(s);
}
@SuppressWarnings("unused")
private void setButtonOnClickListeners(){
re_run.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
}
});
}
@Override
public void onBackPressed() {
// Do nothing
}
}
【问题讨论】:
-
你通过的分数类型和发布日志
-
张贴整个
gameover类 -
按照@DIVA 的建议,提供 Logcat,我们或许可以提供帮助。
-
如果可以的话,发布整个 gameover.class。
标签: java android class android-intent int