Android从另一个活动获取Int

时间:2013-04-18 05:20:32

标签: android

我想要实现的是当我点击一个图像按钮时它将1添加到一个int,然后当我进入stats页面时它会在textview中显示这个int,但当我点击stats按钮时应用程序崩溃。


颜色等级

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_color);

    blue = (ImageButton)findViewById(R.id.imageButton1);


    blue.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            tb1.setText("BLUE");
            bl1++;


        }
    });

    home.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            Intent intent  = new Intent(v.getContext(), MainActivity.class);

            intent.putExtra("blue1", bl1);

            startActivityForResult(intent, 0);


        }
    });

统计类

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_stats);

    theblue = (TextView)findViewById(R.id.textView1);
    theblue.setText(getIntent().getExtras().getInt("blue1"));


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.stats, menu);
    return true;
}

}

04-18 05:30:12.645: W/dalvikvm(790): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-18 05:30:12.685: E/AndroidRuntime(790): FATAL EXCEPTION: main
04-18 05:30:12.685: E/AndroidRuntime(790): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.childapp/com.example.childapp.Stats}: java.lang.NullPointerException
04-18 05:30:12.685: E/AndroidRuntime(790):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-18 05:30:12.685: E/AndroidRuntime(790):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-18 05:30:12.685: E/AndroidRuntime(790):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-18 05:30:12.685: E/AndroidRuntime(790):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-18 05:30:12.685: E/AndroidRuntime(790):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-18 05:30:12.685: E/AndroidRuntime(790):  at android.os.Looper.loop(Looper.java:137)
04-18 05:30:12.685: E/AndroidRuntime(790):  at android.app.ActivityThread.main(ActivityThread.java:5041)
04-18 05:30:12.685: E/AndroidRuntime(790):  at java.lang.reflect.Method.invokeNative(Native Method)
04-18 05:30:12.685: E/AndroidRuntime(790):  at java.lang.reflect.Method.invoke(Method.java:511)
04-18 05:30:12.685: E/AndroidRuntime(790):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-18 05:30:12.685: E/AndroidRuntime(790):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-18 05:30:12.685: E/AndroidRuntime(790):  at dalvik.system.NativeStart.main(Native Method)
04-18 05:30:12.685: E/AndroidRuntime(790): Caused by: java.lang.NullPointerException
04-18 05:30:12.685: E/AndroidRuntime(790):  at com.example.childapp.Stats.onCreate(Stats.java:19)
04-18 05:30:12.685: E/AndroidRuntime(790):  at android.app.Activity.performCreate(Activity.java:5104)
04-18 05:30:12.685: E/AndroidRuntime(790):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-18 05:30:12.685: E/AndroidRuntime(790):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-18 05:30:12.685: E/AndroidRuntime(790):  ... 11 more

1 个答案:

答案 0 :(得分:0)

  1. 首先放置Log以检查值是否正在传入intent。
    如果成功:
  2. 然后使用此代码获取值。

    theblue.setText(""+getIntent().getExtras().getInt("blue1"));