2014-03-31 62 views
-2

我的應用程序出現問題。我正嘗試使用此代碼從活動轉移整數:如何將變量從文件傳輸到另一個

  Intent intent = new Intent(context, Lesson.class); 
      Intent intent1 = new Intent(context, Example1.class); 

      switch(position){              
       case 0: {intent.putExtra("Title", l1);intent1.putExtra("check_cat", position);break;}     
       case 1: {intent.putExtra("Title", l2);intent1.putExtra("check_cat", position);break;}     
       case 2: {intent.putExtra("Title", l3);intent1.putExtra("check_cat", position);break;}     
       case 3: {intent.putExtra("Title", l4);intent1.putExtra("check_cat", position);break;}     
       case 4: {intent.putExtra("Title", l5);intent1.putExtra("check_cat", position);break;}     
       case 5: {intent.putExtra("Title", l6);intent1.putExtra("check_cat", position);break;}     

第一個意圖有效,但第二個意圖不起作用。我沒有收到任何數據。這裏就是我處理信息的代碼:

final int[] pos_categ = new int[1]; 
    pos_categ[0] = getIntent().getExtras().getInt("check_cat"); 

然後我嘗試使用它作爲一個條件的「if語句」:

if (pos_categ[0]==1){ 
     title[0] = lessons_titles[position[0]]; 
     eng[0] = eng_version[position[0]]; 
     dan[0] = dan_version[position[0]].toLowerCase(); 
    } 
    if (pos_categ[0]==2){ 
     title[0] = lessons_titles2[position[0]]; 
     eng[0] = eng_version2[position[0]]; 
     dan[0] = dan_version2[position[0]].toLowerCase(); 
    } 
    if (pos_categ[0]==3){ 
     title[0] = lessons_titles3[position[0]]; 
     eng[0] = eng_version3[position[0]]; 
     dan[0] = dan_version3[position[0]].toLowerCase(); 
    }... 

我沒有得到任何錯誤,但是從應用程序是沒有通過if語句,因爲變量pos_categ [0]沒有任何值...有人可以幫我解決這個問題嗎?

+1

但爲什麼兩個'Intent's?你是否將兩個「意圖」發送給一個「活動」? (這實際上是不可能的) – EdmDroid

+0

我發送到兩個不同的活動。(Lesson.class和Example1.class) –

+0

在你的文章中哪些代碼片段屬於哪個? – EdmDroid

回答

1

你需要做兩件事 每次他們是一個意圖首先檢查。

Intent intent = getIntent(); 
if(intent.hasExtra("check_cat")) 
{ 
// your code 
} 

秒你需要把int作爲常規int。

final int pos_categ = getIntent().getExtras().getInt("check_cat"); 
0

試試這個

final int[] post_categ = new int[] {getIntent().getExtras().getInt("check_cat")}; 

或者更好的是:

Intent intent = getIntent(); 
int pos_categ = 0; 
if(intent.hasExtra("check_cat")) { 
    pos_categ = intent.getExtras().getInt("check_cat"); 
} 
相關問題