Android SQLITE 图像问题:尝试在空对象引用上调用虚拟方法 'java.lang.String android.net.Uri.toString()'

Android SQLITE Image issue: Attempt to invoke virtual method 'java.lang.String android.net.Uri.toString()' on a null object reference

问题:如果用户不上传任何图片,应用程序崩溃,因为无法存储以前的图片...

概述:我的应用程序由类别和编辑器活动组成。

尝试在空对象引用上调用虚方法'java.lang.String android.net.Uri.toString()'

如果用户再次上传图片 - 相同或不同,都没有关系,应用程序运行良好。

如果用户不想上传新图像,目标是保留图像,就像应用程序保留 EditText 字段的值一样:

 private void saveInventory() {

// Read from input fields
// Use trim to eliminate leading or trailing white space
String nameString = mNameEditText.getText().toString().trim();
String infoString = mAdditionalInfoEditText.getText().toString().trim();
String priceString = mPriceEditText.getText().toString().trim();
String quantityString = mQuantityTextView.getText().toString().trim();
String image = actualUri.toString(); <---- error here

此方法选择图片:

  @Override
    public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
        // The ACTION_OPEN_DOCUMENT intent was sent with the request code READ_REQUEST_CODE.
        // If the request code seen here doesn't match, it's the response to some other intent,
        // and the below code shouldn't run at all.

        if (requestCode == SELECT_AN_IMAGE_REQUEST && resultCode == Activity.RESULT_OK) {
            // The document selected by the user won't be returned in the intent.
            // Instead, a URI to that document will be contained in the return intent
            // provided to this method as a parameter.  Pull that uri using "resultData.getData()"

            if (resultData != null) {
                actualUri = resultData.getData();
                mPhotoImageView.setImageURI(actualUri);

            }
        }
    }

我相信 s保存 activity 状态并恢复它 可能会解决问题(下面的代码),但我不知道将其粘贴到哪里我的代码....

    // Save the activity state when it's going to stop.
   // @Override
   // protected void onSaveInstanceState(Bundle outState) {
      //  super.onSaveInstanceState(outState);

       // outState.putParcelable("actualUri", actualUri);
   // }

    // Recover the saved state when the activity is recreated.
   // @Override
  //  protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
    //    super.onRestoreInstanceState(savedInstanceState);

     //   actualUri= savedInstanceState.getParcelable("actualUri");

   // }

可能有人知道如何在保存编辑器时保持相同的图像activity。谢谢。

P.S。 finish activity 退出编辑器,returns 进入目录。所有数据都保存在数据库中 - 除了图像。

    @Override public boolean onOptionsItemSelected(MenuItem item) { 
// User clicked on a menu option in the app bar overflow menu switch (item.getItemId()) 
{ // Respond to a click on the "Save" menu option case R.id.action_save: 
// Save to database saveInventory(); 
// Exit activity finish(); return true;

我已经解决了这个问题。

我不得不删除在 'Save' class 中导致错误的行,并在 class:

中创建一个单独的验证
 if (actualUri== null) {
            Toast.makeText(this, getString(R.string.image_required), Toast.LENGTH_SHORT).show();
            return hasAllRequiredValues;
        } else {
            values.put(InventoryEntry.COLUMN_INVENTORY_IMAGE, actualUri.toString());
        }