2013-04-18 111 views
-2

我想要實現的是當我點擊一個圖像按鈕時,它將int加1,然後當我進入stats頁面時,它將在textview中顯示此int,但是當我點擊我的應用程序崩潰的統計按鈕。Android從另一個活動獲取Int


顏色類

@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 
+0

它是如何崩潰的?請提供一個logcat。 – zmbq 2013-04-18 05:21:50

+0

你在說什麼按鈕?具體並告訴我們logcat,我們無法讀取您的整個代碼, – 2013-04-18 05:25:46

+0

有幫助嗎? – steven 2013-04-18 05:31:05

回答

0
  1. 首先把日誌檢查值是否通過意圖傳遞。
    如果成功:
  2. 然後使用此代碼獲取值。

    theblue.setText(""+getIntent().getExtras().getInt("blue1")); 
    
相關問題