2013-09-27 32 views
-3

我有兩個類page1和page2。從第1頁第2頁中的第1頁中點擊按鈕如何將整數數據從page1傳輸到page2?

時我怎樣才能將數據傳輸到整數我已經試過了幾個代碼: Page1.Class

protected void onListItemClick(ListView l, View v, int position, long id) { 
     Intent i = null; 
     switch (position) 
     { 
     case 0: 
      i = new Intent(this, page2.class); 
     i.putExtra("Konum", 2); 
     break; 

     } 
     startActivity(i); 

Page2.Class

Intent intent = getIntent(); 
    private Integer konum = intent.getIntExtra("Konum", 0); 

但不工作!

09-27 19:50:19.556: E/AndroidRuntime(528): FATAL EXCEPTION: main 
09-27 19:50:19.556: E/AndroidRuntime(528): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{ru.truba.touchgallery/ru.truba.touchgallery.GalleryUrlActivity}: java.lang.NullPointerException 
09-27 19:50:19.556: E/AndroidRuntime(528): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569) 
09-27 19:50:19.556: E/AndroidRuntime(528): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
09-27 19:50:19.556: E/AndroidRuntime(528): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
09-27 19:50:19.556: E/AndroidRuntime(528): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
09-27 19:50:19.556: E/AndroidRuntime(528): at android.os.Handler.dispatchMessage(Handler.java:99) 
09-27 19:50:19.556: E/AndroidRuntime(528): at android.os.Looper.loop(Looper.java:123) 
09-27 19:50:19.556: E/AndroidRuntime(528): at android.app.ActivityThread.main(ActivityThread.java:3683) 
09-27 19:50:19.556: E/AndroidRuntime(528): at java.lang.reflect.Method.invokeNative(Native Method) 
+2

「但它不起作用」你是什麼意思?你有什麼錯誤嗎? – Emmanuel

+0

請在您的問題中添加更多詳細信息,以便人們可以回答。應用程序崩潰了嗎?然後發佈崩潰日誌。價值不正確?然後張貼你在第二個意圖中收到的價值。目前,想要弄清楚你在問什麼是非常困難的。 –

+0

http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android可以幫助 – justadeveloper

回答

0
Intent i = new Intent(this, ActivityTwo.class); 
    i.putExtra("Value1", 1); 
    startActivity(i); 

並且在其他活動之後的OnCreate()

Bundle extras = getIntent().getExtras(); 
int value1 = extras.getint("Value1",0); 
怎樣從第1頁在第2頁中第1頁的按鈕被點擊

EROR時將數據傳輸到整數

看到這個詳細的教程Android Intents

+2

爲什麼這與OP正在做的事情大不相同,爲什麼它會解決這個問題?發佈的代碼看起來不錯,但沒有logcat或問題描述,我們無法確定。通常情況下,我懷疑有問題的代碼沒有被顯示。 – Simon

+0

好,對我也有用謝謝 –

相關問題