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

问题描述 投票:0回答:1

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

概述:我的应用程序包含类别和编辑器活动。

  • 编辑器活动有一个按钮来上传图像并保存活动。
  • 类别活动显示编辑器活动中的图像。
  • 问题在这里 - >一旦用户返回编辑编辑器活动中的详细信息,应用程序崩溃时出现以下错误:

尝试在空对象引用上调用虚方法'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);

            }
        }
    }

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

    // 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");

   // }

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

附:完成活动退出编辑器,并返回目录。除图像外,所有数据都保存在数据库中。

    @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;
android android-lifecycle
1个回答
0
投票

我已经解决了这个问题。

我不得不删除导致“保存”类中出错的行,并在此类中创建单独的验证:

 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());
        }
© www.soinside.com 2019 - 2024. All rights reserved.