android - 如何从一个活动中获取选定的图像到来自firebase的第二个活动
问题描述
我是 android 开发的新手我想通过从第一个活动中获取选定的图像来在另一个片段中显示图像但我收到错误
这是我的网格视图,用户在其中选择图像
@Override
public void onGridImageSelected(Photo photo, int activityNumber) {
Log.d(TAG, "onGridImageSelected: selected an image gridview: " + photo.toString());
FullScreenProductFragment fragment = new FullScreenProductFragment();
Bundle args = new Bundle();
args.putParcelable(getString(R.string.photo), photo);
args.putInt(getString(R.string.activity_number), activityNumber);
fragment.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.Profilecontainer, fragment);
transaction.addToBackStack(getString(R.string.view_post_fragment));
transaction.commit();
}
这是应查看的所选图像
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragmentfullscreenlayout, container, false);
mImageView = view.findViewById(R.id.zoom_image);
Bundle bundle = getActivity().getIntent().getExtras();
UnivarsalImageLoader.setImage(getPhotoFromBundle().getImage_path(),mImageView, null, "");
Log.d(TAG, "onCreateView: getphoto from bundle"+getPhotoFromBundle().getImage_path());
return view;
}
private Photo getPhotoFromBundle(){
Log.d(TAG, "getPhotoFragment: argumentd"+getArguments());
Bundle bundle = this.getArguments();
if (bundle != null){
return bundle.getParcelable(getString(R.string.photo));
}else {
return null;
}
}
这是我的日志
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.alpha.lapid.models.Photo.getImage_path()' on a null object reference
at com.example.alpha.lapid.Utils.FullScreenProductFragment.onCreateView(FullScreenProductFragment.java:56)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2346)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.j va:1428)
当我在片段中记录这个 getphotobundle 时,我没有得到任何东西,问题出在哪里,请告诉我
05-20 08:52:23.534 668-668/com.example.alpha.lapid
D/FullScreenProductFragme: getPhotoFragment: argumentdBundle[{}]
解决方案
Bundle bundle = getActivity().getIntent().getExtras();
Photo photo = getPhotoFromBundle(bundle);
if (photo==null) {
Toast( ... Photo is null ..);
return;
}
String path = photo.getImage_path();
File file = new File(path);
if (!file.exists()) {
Toast( .. file does not exist ..);
return;
}
UnivarsalImageLoader.setImage(file.getAbsolutePath(), mImageView, null, "");
推荐阅读
- javascript - 如何使用拆分为数组的字符串搜索数组?
- excel - excel在end with和for循环之间崩溃?
- linux - faillock 在 RHEL6 上将正确的密码标记为错误
- python-3.x - Python3 使用自制软件安装但未使用更新版本
- animation - 动画 SVG 中的文本元素以口吃的方式移动
- android - 为动态视图设置数据
- acumatica - 如何在 Web 服务端点中通过或条件 - Acumatica
- c# - 日期不会在 C# 的 Unix 时间戳中转换
- buildroot - 控制台登录,无密码提示 => 从用户提示到用户提示
- azure-data-factory - 使用 azure 数据工厂查找 Azure 数据湖目录中可用的文件数