android - Android SQLITE 图像问题:尝试在空对象引用上调用虚拟方法“java.lang.String android.net.Uri.toString()”
问题描述
问题:如果用户不上传任何图像,应用程序崩溃,因为无法存储以前的图像......
概述:我的应用程序由类别和编辑器活动组成。
- 编辑器活动有一个按钮来上传图像和保存活动。
- 类别活动显示来自编辑器活动的图像。
- 问题在这里->一旦用户返回编辑器活动中编辑详细信息,应用程序崩溃并出现以下错误:
尝试在空对象引用上调用虚拟方法“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");
// }
可能有人知道如何在保存编辑器活动时保持相同的图像。谢谢你。
PS完成活动退出编辑器,并返回目录。所有数据都保存在数据库中 -除了图像。
@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;
解决方案
我已经解决了这个问题。
我必须删除在“保存”类中导致错误的行,并在此类中创建单独的验证:
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());
}
推荐阅读
- python - 如果并行调用,Redis Pipeline 需要时间
- vue.js - vue-croppa:获取原始图像并用作另一个 Croppa 组件的初始图像
- swift - 自定义 UITextField 类中的 draw(_ rect: CGRect) 不要在 ios 14 上调用
- java - 将目标 sdk 升级到 30 时初始化 youtube 播放器时出错
- javascript - Laravel HTML Select输入在数据库中保存不同的值?
- python - 缩短字符串/数组索引
- ruby-on-rails - 如何在rails中为数组类编写方法
- django - 如何修复heroku中的应用程序响应错误?
- python - 如何使用 Pytest 断言多个值
- javafx - Gluon Scene Builder:来自源代码的自定义控件