我正在使用片段使用intents打开相机。但是当我使用位图设置我的ImageView时,我得到一个空指针异常。
CODE
public class UploadDocument extends MasterFragment implements View.OnClickListener {
private TextView label;
private Button btChooseDoc, btDocumentType, btUpload, btCancel;
private EditText etTitle;
private String[] chooseDocumentArray;
private String userSelection = "";
ImageView image;
private Intent intent;
Bitmap camera;
public void onClick(View view) {
setUp();
switch (view.getId()) {
case R.id.bt_choose_file:
displayPopup(getActivity(), "Choose File", chooseDocumentArray, btChooseDoc, false, new GetNamePosition() {
@Override
public void getName(String name) {
userSelection = name;
if (userSelection.equals("Gallery")) {
intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 0);
btChooseDoc.setText("Choose File");
}
if (userSelection.equals("Camera")) {
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);
btChooseDoc.setText("Choose File");
}
if (userSelection.equals("Other")) {
intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent, 2);
btChooseDoc.setText("Choose File");
}
}
@Override
public void getPosition(int position) {
}
});
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==1 && resultCode==getActivity().RESULT_OK){
camera= (Bitmap) data.getExtras().get("data");
camera.compress(Bitmap.CompressFormat.JPEG,100,null);
image.setImageBitmap(camera);
}
}
logcat的
Caused by: java.lang.NullPointerException
at android.graphics.Bitmap.compress(Bitmap.java:890)
at example.com.pocketdocs.UploadDocument.UploadDocument.onActivityResult(UploadDocument.java:108)
at android.app.Activity.dispatchActivityResult(Activity.java:5315)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3529)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3576)
at android.app.ActivityThread.access$1100(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5370)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
我正在使用片段使用intents打开相机。但是当我使用位图设置我的ImageView时,我得到一个空指针异常。 我已经声明了上面的位图,但我仍然是空指针
答案 0 :(得分:0)
你可以试试这个 -
image.post(
new Runnable(){
image.setImageBitmap(camera)
});
需要logcat数据来提供具体的解决方案。
答案 1 :(得分:0)
由于您没有发布log cat
。我认为问题发生了
camera= (Bitmap) data.getExtras().get("data");
camera
是null
。因此,您获得NPE
。这应该是
camera= (Bitmap) data.getExtra("data");
还会更改onActivityResult(...)
if(requestCode==1 && resultCode==getActivity().RESULT_OK && data != null)
尝试recycle
您使用过的位图,否则会导致outofmemoryexception
image.setImageBitmap(camera);
camera.recycle();