2013-09-21 45 views
1

我試圖發送一個基本上包含設備上文件路徑的array of Strings。首先是用戶Selects Picture 1,然後是Select Picture 2。一旦用戶完成該數組然後被加載並傳遞到下一個活動。當試圖收到變量返回NullPointer在Android中的兩個活動之間傳遞String Array時的NullPointer

MainActivity:

case SELECT_PICTURE1: 
    if (resultCode == RESULT_OK) { 
     // code here that gets the image path 
     // String leftImagePath contains the path of selected Image 
     // Used toast to display the image path and it is not null 
     finished = true; 
    } 
    break; 

case SELECT_PICTURE2: 
    if (resultCode == RESULT_OK) { 
     //similar to Select Picture 1 
     // String rightImagePath contains the path of selected Image 
     if (finished == true) { 
      Bundle b = new Bundle(); 
      b.putStringArray("stringArray", new String[] { 
       LeftImageString, RightImageString }); 
      Intent i = new Intent(MainActivity.this, modifiedImage.class); 
      i.putExtras(b); 
      // finished = false; 
     } 
    } 
    break; 

ModifiedImage類:

Intent intent = getIntent(); 
_imagesPath = intent.getStringArrayExtra("IMAGES_PATH"); 
Bundle b= this.getIntent().getExtras(); 
_userImagePath = b.getStringArray("stringArray"); 


if (_imagesPath == null) { 
    for (int i = 0; i <= 1; i++) { 
     //null pointer on the following line because _userImagePath contains nothing. 
     _imagesPath[i] = _userImagePath[i]; 
     _originalBitmaps[i] = BitmapFactory.decodeFile(_imagesPath[i]); 
    } 
} 

有誰知道它是什麼,我做錯了?

+0

你在哪裏傳遞'從第一Activity.also IMAGES_PATH'鍵獲得軟件包之前添加null檢查和第二個活動的意圖值 –

+0

@ User1204501我認爲他在他的評論 – Trikaldarshi

回答

2

此代碼是明顯的打破:

if (_imagesPath == null) { 
    for (int i = 0; i <= 1; i++) { 
     _imagesPath[i] = _userImagePath[i]; 
     _originalBitmaps[i] = BitmapFactory.decodeFile(_imagesPath[i]); 
    } 
} 

如果你進入if語句的身體,你知道_imagesPath爲空 - 但你取消對它的引用在循環,而不必爲變量分配新的值。在我看來,你想只克隆陣列:

if (_imagesPath == null) { 
    _imagesPath = _userImagePath.clone(); 
} 

...甚至只是複製參考:

if (_imagesPath == null) { 
    _imagesPath = _userImagePath; 
} 

要那麼無條件執行下面這行我懷疑代碼爲每個適當的值i - 爲什麼你只想要這樣做,如果_imagesPath以前是空的?

_originalBitmaps[i] = BitmapFactory.decodeFile(_imagesPath[i]); 

(這不是我清楚你在哪裏初始化_originalBitmaps要麼,但那是另一回事。)

+0

謝謝先生!將在早上第一件事嘗試。 – User1204501

+0

如果路徑爲空,我檢查路徑的原因是因爲用戶有兩個選擇。從相機捕捉圖像,在這種情況下,圖像路徑自動加載或從位置選擇圖像。有沒有另一種方式呢? – User1204501

+0

@ User1204501您必須檢查空值然後才能檢查值 – Trikaldarshi

1

你不傳遞任何IMAGES_PATH的額外那麼你爲什麼希望在目標活動的價值

Intent intent = getIntent(); 
    _imagesPath = intent.getStringArrayExtra("IMAGES_PATH"); 
    Bundle b= this.getIntent().getExtras(); 
    _userImagePath = b.getStringArray("stringArray"); 

如果_imagesPath爲空,那麼你做的_imagesPath就像你在代碼下面是每個會扔NPE

只要添加一個不是'!'改變了一切

if (_imagesPath != null) { 

    for (int i = 0; i <= 1; i++) { 
     //null pointer on the following line because _userImagePath contains nothing. 
     _imagesPath[i] = _userImagePath[i]; 
     _originalBitmaps[i] = BitmapFactory.decodeFile(_imagesPath[i]); 
    } 
} 
+0

中很清楚IMAGE_PATH已經從MainActivity傳遞過來。我只是沒有包含完整的代碼,因爲這不會導致錯誤。 – User1204501

+0

好的,但你應該檢查**!= null **不適用** == null ** – Trikaldarshi

相關問題