Android:使用Intent崩溃应用程序传递Int

时间:2014-08-20 19:51:08

标签: java android class android-intent int

您希望有人可以帮我解决运行代码时遇到的错误。当用户从主要类别获得游戏而得到的游戏被带到游戏类以显示最终得分时,需要什么。没有意图游戏在屏幕上加载正常但是当我在gameover运行游戏时添加意图应用程序崩溃这里是我的代码,其中得分是int:

主要意图:

Intent myIntent = new Intent(MainActivity.this,gameover.class);
            myIntent.putExtra("number", score);
                        MainActivity.this.startActivity(myIntent); 

gameover class:

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
      }
    }

2 个答案:

答案 0 :(得分:0)

把这一行 -

   re_run = (Button) findViewById(R.id.retry);

下面

setContentView().

答案 1 :(得分:0)

TextView EndScore布局中找不到R.layout.gameover(如果将来可以使用变量的小写字母)。

确保TextView实际上在该XML文件中。 请确保它是TextView而不是其他内容,例如EditText或类似内容(尽管我认为这会引发不同的错误)。

根据LogCat,

EndScore为空。