在Bundle getExtras()方法中获取空对象引用

时间:2015-12-24 20:22:19

标签: android

我正在创建一个应用程序,它可以从相机中捕获图像并将图像保存在文件中。在 onActivityResult()方法中,我得到空对象引用错误。 My Fragment课程在这里:

public class HomeFragment extends Fragment {
private static final int REQUEST_IMAGE_CAPTURE = 1;
private static final String JPEG_FILE_PREFIX = "IBR_";
private static final String JPEG_FILE_SUFFIX = ".jpg";
File photoFile = null;
ImageButton camera_button;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_home, container, false);
    camera_button = (ImageButton) rootView.findViewById(R.id.camera);
    camera_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            // Ensure that there's a camera activity to handle the intent
            if (cameraIntent.resolveActivity(getActivity().getPackageManager()) != null) {
                // Create the File where the photo should go
                try {
                    photoFile = createImageFile();
                } catch (IOException ex) {
                    // Error occurred while creating the File
                    Log.i("File error", "File");
                }
            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                Uri fileUri = Uri.fromFile(photoFile);
                cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
                        fileUri);
                Toast.makeText(getActivity(), photoFile.toString(), Toast.LENGTH_SHORT).show();
                startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
            }
        }
    });

    return rootView;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
        camera_button.setImageBitmap(imageBitmap);
    }

}

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = JPEG_FILE_PREFIX + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File imageF = File.createTempFile(imageFileName, JPEG_FILE_SUFFIX, storageDir);
    return imageF;
}

}

实际上我在这一行收到错误 Bundle extras = data.getExtras(); 有什么建议我怎么解决这个问题?

1 个答案:

答案 0 :(得分:1)

如果你指定Mesh.Data.VertexBuffer.Length := 4; Mesh.Data.IndexBuffer.Length := 6; with Mesh.Data.VertexBuffer do begin Vertices[0] := TPoint3D.Create( 1, 1, 0); Vertices[1] := TPoint3D.Create( 1, -1, 0); Vertices[2] := TPoint3D.Create(-1, -1, 0); Vertices[3] := TPoint3D.Create(-1, 1, 0); TexCoord0[0] := TPointF.Create(0, 1); TexCoord0[1] := TPointF.Create(0, 1); TexCoord0[2] := TPointF.Create(0, 1); TexCoord0[3] := TPointF.Create(0, 1); end; with Mesh.Data.IndexBuffer do begin Indices[0] := 0; Indices[1] := 1; Indices[2] := 2; Indices[3] := 2; Indices[4] := 3; Indices[5] := 0; end; ,那么在intentOnResult中没有返回意图额外的拇指选择 - 你必须手动读取你提供的MediaStore.EXTRA_OUTPUT文件路径。

您可以阅读ACTION_IMAGE_CAPTURE的文档:

  

调用者可以传递额外的EXTRA_OUTPUT来控制该图像的写入位置。 *如果EXTRA_OUTPUT不存在,则在额外字段中返回一个小尺寸图像作为Bitmap对象。