在不同的包之间发送字符串时的NullPointer

时间:2013-10-13 13:12:10

标签: java android exception nullpointerexception

我正在尝试将_imagesPaths的字符串数组从一个包传递到另一个包。

我尝试了以下内容:

//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;     Bundle extras = 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.

3 个答案:

答案 0 :(得分:0)

在为其设置值之前,必须初始化_imagesPath:

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

答案 1 :(得分:0)

你可以直接传递_imagesPaths,如

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

并且可以在另一端获得它,

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

答案 2 :(得分:0)

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

谢谢。