【问题标题】:Null pointer exception when reading images from database second time第二次从数据库读取图像时出现空指针异常
【发布时间】:2012-05-25 10:00:32
【问题描述】:

我从我的 sdcard 中读取图像并将路径存储在一个活动内的数组中,它工作正常,这个读取在一个方法内。第二次,我从另一个活动中调用相同的方法,但光标返回 null ......我可以说问题出在“this”,但不知道在哪里改变。下面是方法的代码。

 public void LoadImagesFromSDCard()
{
    try
    {
        String[] projection = {MediaStore.Images.Media.DATA};           

        ContentResolver cr = this.getContentResolver();

        cursor = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,projection,null,null,null);
        imageCount = cursor.getCount();
        imagePath = new String[imageCount + 1];
        cursor.moveToFirst();

        int cursor_index = 0;
        do
        {
                int id = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
                imagePath[cursor_index++] = cursor.getString(id);

        }while(cursor.moveToPosition(cursor_index));

     ........

我第二次从像这样的另一个活动中调用它

    Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
    public void onPictureTaken(byte[] imageData, Camera c) {


        if (imageData != null) {

            Intent mIntent = new Intent();

            StoreByteImage(mContext, imageData, 50,"ImageName");
            mCamera.startPreview();
            setResult(FOTO_MODE, mIntent);
            CameraTaken = true;

            IS.LoadImagesFromSDCard();
            finish();


        }
    }
};

实际上这是信息的相机活动。提前感谢您的帮助:)

这里是日志猫

E/CameraTest(19169): onCreate
E/CameraTest(19169): onResume
E/CameraTest(19169): surfaceCreated
D/CameraHardwareStub(   34): initHeapLocked: preview size=320x240
E/CameraTest(19169): surfaceChanged
D/CameraHardwareStub(   34): initHeapLocked: preview size=320x240
I/ActivityManager(   59): Displayed activity com.android.print/.CameraActivity: 1013   ms (total 1013 ms)
D/AudioSink(   34): bufferCount (4) is too small and increased to 12
I/global  (19169): Default buffer size used in BufferedOutputStream constructor. It would be better to be explicit if an 8k buffer is required.
E/Error reading file(19169): java.lang.NullPointerException
E/CameraTest(19169): surfaceDestroyed
E/CameraTest(19169): onStop

【问题讨论】:

  • IS.LoadImagesFromSDCard() 中的 IS 是什么;.........
  • IS 是具有 LoadFrom...().... 方法的活动的对象。
  • 你可以将函数设为静态。否则你可能永远无法从外部访问!
  • 如果我将其设为静态,我无法在图片回调中创建意图

标签: android database nullpointerexception android-sdcard


【解决方案1】:
【解决方案2】:

好吧,我想我已经解决了这个问题,因为我所做的工作有效。 关键是我的第一个活动在后台,我从第一个调用相机活动,这使得第一个活动进入后台。所以我不得不为清单文件中的第一个活动添加 android:launchMode="singleTask" 。然后我明确调用在后台运行的第一个活动,而不是 IS.LoadImagesFromSDCard();。这样做会在第一个活动中调用 onNewIntent() 而不是 onCreate()。在 onNewIntent() 中,我调用了我的 LoadFrom...() 方法。

    Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
    public void onPictureTaken(byte[] imageData, Camera c) {


        if (imageData != null) {

            Intent mIntent = new Intent();
            Intent inent = new Intent();

            StoreByteImage(mContext, imageData, 50,"ImageName");
            mCamera.startPreview();
            setResult(FOTO_MODE, mIntent);
            CameraTaken = true;
            ImageSelectionIntent.setClassName("your package name", "your package name + class name");
            startActivity(intent);
            finish();

我的第一个活动有以下代码

 protected void onNewIntent(Intent intent) {
      super.onNewIntent(intent);
      setIntent(intent);        
      LoadImagesFromSDCard();
    }
        }
    }
};

参考下面的链接

communicating between two activities while the one called the other still running in background

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-04
    • 2017-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多