【发布时间】:2018-01-21 06:07:00
【问题描述】:
我有一个名为PhotoManager 的类,它处理获取适当权限、访问相机或图库并返回Bitmap。此类从需要更新用户个人资料图片等的其他类中调用,就像这样
public void onClick(DialogInterface dialog, int which) {
//user wants to take a picture
profilePic.setImageBitmap(photoManager.userWantsToTakePicture());
}
我在 PhotoManager 中调用 startActivityForResult(),但它不起作用。这是我的功能
public Bitmap userWantsToUploadPicture(){
int permissionChecker = ContextCompat.checkSelfPermission(context, android.Manifest.permission.READ_EXTERNAL_STORAGE);
if(permissionChecker == PackageManager.PERMISSION_GRANTED){
//THIS NEXT LINE DOES NOT WORK
startActivityForResult(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI), SELECT_FILE);
return selectedImage;
}
else{
--do stuff
}
}
我不断收到此错误
java.lang.NullPointerException: Attempt to invoke virtual method
'android.app.ActivityThread$ApplicationThread
android.app.ActivityThread.getApplicationThread()' on a null object
reference
我不知道为什么我的意图是空的,因为我已经初始化了它。
另外,我在更新用户个人资料图片的类中运行了这段代码,并且运行良好。当我从 PhotoManager 类运行代码时,这只是一个问题
PhotoManager() 有一个构造函数
public PhotoManager(Context c, Activity a){
context = c;
activity = a;
}
【问题讨论】:
-
在这种情况下
context是什么?它来自哪里? -
抱歉,上下文是
classname.this,如果有意义的话,我从调用photoManager的类传入 - 我刚刚编辑了我的 Q
标签: android-activity start-activity startactivityforresult