2013-10-13 30 views
0

我试图从一个包传递一个包到另一个包的_imagesPaths字符串数组。NullPointer在不同的包之间发送字符串时

我曾尝试以下:

//sending the paths of images from `MainActivity` which is in `main.packages` 
//assume the array is not null 

Intent b = new Intent(MainActivity.this, EditPicturesActivity.class); 
b.putExtra("left",LeftImageString); 
b.putExtra("right",RightImageString); 

路径在另一个包通过执行以下操作接收: private String[] _imagesPath; 捆绑额外= getIntent()getExtras();
_imagesPath [0] = extras.getString(“left”); _imagesPath [1] = extras.getString(“right”);

接下来,我尝试加载路径提供的图像,但我得到一个NullPointer,其中说_imagesPath is null

编辑

_imagesPath的值是通过做从图库中选择图片分配: 在本次活动

private String[] _imagesPath = null;

case SELECT_PICTURE1: 
     if (resultCode == RESULT_OK) { 

      Uri selectedImage = data.getData(); 
      String[] filePathColumn = { MediaStore.Images.Media.DATA }; 

      Cursor cursor = getContentResolver().query(selectedImage, 
        filePathColumn, null, null, null); 
      cursor.moveToFirst(); 

      int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
      LeftImageString = cursor.getString(columnIndex); 

      cursor.close(); 

      //the toast displays the path and it is not null 
      Toast.makeText(
        getApplicationContext(), 
        "The path of the first image you have selected is: " 
          + LeftImageString, Toast.LENGTH_SHORT).show(); 
      // String leftImagePath contains the path of selected Image 


      //intent for "left" is placed here 

     }  
     break; 

//similary image is taken for Image 2. 
+0

你在哪里给'_imagesPath'设置了值? – BobTheBuilder

+0

如何初始化_imagesPath? – hasan83

+0

您正在将_imagespath设置为null。为什么你会惊讶地发现一个空的异常? – Kuffs

回答

0

必须初始化_imagesPath设置值前它:

String _imagesPath[] = new String[2]; 
Bundle extras = getIntent().getExtras();    
_imagesPath[0] = extras.getString("left"); 
_imagesPath[1] = extras.getString("right"); 
0

你可以直接传似_imagesPaths,

Intent b = new Intent(MainActivity.this, EditPicturesActivity.class); 
b.putExtra("paths",_imagesPaths); 

而且可以得到它另一端一样,

String[] paths = getIntent().getStringArrayExtra("paths"); 
+0

是的,我也试过,买它继续给出相同的错误 – User1204501

0

你的代码是好的,但你需要初始化你的图像阵列。

谢谢。

+0

我只是做了哈桑的答案状态 – User1204501

+0

仍然无法正常工作 – User1204501

+0

是的,因为在你的代码只有这个错误。 – Yogendra

相关问题