从android中的片段拍摄照片时出现Nullpointer异常

时间:2013-11-20 04:56:28

标签: android android-intent

有人可以告诉我为什么这段代码会给我一个空指针异常

ImageFradment用于启动摄像头意图。此片段托管在另一个活动

1

public class ImageFragment extends Fragment{
private Uri imageUri;
private String mPath;
private ImageView image;
Bitmap bitmap = null;
private File tempPhoto;
 @Override 
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
   { 
     View v = inflater.inflate(R.layout.fragment_image, container, false);
     ImageButton snap=((ImageButton)v.findViewById(R.id.snap));
     image = ((ImageView) v.findViewById(R.id.image));
        snap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {              

                Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                String path = Environment.getExternalStorageDirectory()
                        + "/spot";
                File photopath = new File(path);
                if (!photopath.exists()) {
                    photopath.mkdir();
                }

                File imagePath = new File(photopath, System.currentTimeMillis()+ ".png");
                intent.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(imagePath));
                imageUri = Uri.fromFile(imagePath);

                getActivity().startActivityForResult(intent, 100);

            }
        });

     return v;
  }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if( requestCode == 100 ) 
        {

            imageUri = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getActivity().getContentResolver().query(imageUri,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();

            image.setImageBitmap(BitmapFactory.decodeFile(picturePath));*/


        else {

             bitmap = BitmapFactory.decodeFile(mPath);
         }

   }

错误日志:

1-20 12:25:43.151: E/AndroidRuntime(11717): FATAL EXCEPTION: main
11-20 12:25:43.151: E/AndroidRuntime(11717): java.lang.RuntimeException: Unable to resume activity {com.example.makemyday/com.example.makemyday.ActionDetailActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=131073, result=-1, data=null} to activity {com.example.makemyday/com.example.makemyday.ActionDetailActivity}: java.lang.NullPointerException
11-20 12:25:43.151: E/AndroidRuntime(11717):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2790)
11-20 12:25:43.151: E/AndroidRuntime(11717):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2819)
11-20 12:25:43.151: E/AndroidRuntime(11717):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2266)
11-20 12:25:43.151: E/AndroidRuntime(11717):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3740)
11-20 12:25:43.151: E/AndroidRuntime(11717):    at android.app.ActivityThread.access$700(ActivityThread.java:141)
11-20 12:25:43.151: E/AndroidRuntime(11717):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
11-20 12:25:43.151: E/AndroidRuntime(11717):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-20 12:25:43.151: E/AndroidRuntime(11717):    at android.os.Looper.loop(Looper.java:137)
11-20 12:25:43.151: E/AndroidRuntime(11717):    at android.app.ActivityThread.main(ActivityThread.java:5103)
11-20 12:25:43.151: E/AndroidRuntime(11717):    at java.lang.reflect.Method.invokeNative(Native Method)
11-20 12:25:43.151: E/AndroidRuntime(11717):    at java.lang.reflect.Method.invoke(Method.java:525)
11-20 12:25:43.151: E/AndroidRuntime(11717):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-20 12:25:43.151: E/AndroidRuntime(11717):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-20 12:25:43.151: E/AndroidRuntime(11717):    at dalvik.system.NativeStart.main(Native Method)
11-20 12:25:43.151: E/AndroidRuntime(11717): Caused by: java.lang.RuntimeException: Failure delivering result 

2 个答案:

答案 0 :(得分:0)

看起来你的mPath变量在调用时从不初始化     bitmap = BitmapFactory.decodeFile(mPath);

答案 1 :(得分:0)

当'getActivity()。startActivityForResult();'它会调用onActivityResult()父活动Fragment功能。 不要在onclick侦听器中调用getActivity().startActivityForResult(intent, 100);,而是使用startActivityForResult(intent, 100);。这将解决问题。修改后的oncreate()代码将为

@Override 
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
 { 
     View v = inflater.inflate(R.layout.fragment_image, container, false);
     ImageButton snap=((ImageButton)v.findViewById(R.id.snap));
     image = ((ImageView) v.findViewById(R.id.image));
     snap.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {              

            Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
            String path = Environment.getExternalStorageDirectory()
                    + "/spot";
            File photopath = new File(path);
            if (!photopath.exists()) {
                photopath.mkdir();
            }

            File imagePath = new File(photopath, System.currentTimeMillis()+ ".png");
            intent.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(imagePath));
            imageUri = Uri.fromFile(imagePath);

            getActivity().startActivityForResult(intent, 100);

        }
    });

 return v;
}
相关问题