Android无法使用已实施的删除按钮删除ImageView

时间:2015-07-31 08:42:02

标签: android android-intent imageview

*已编辑*

public void btnTakePhotoClicked(View v) {
    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(cameraIntent, CAMERA_REQUEST);
}

public void btnDeletePhotoClicked(View v) {
    iv = (ImageView) findViewById(R.id.imgSpecimenPhoto);
    iv.setImageDrawable(null);
    Toast.makeText(AddIncome.this, "Photo deleted", Toast.LENGTH_SHORT).show();

}

protected void onActivityResult (int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK) {
        if (requestCode == CAMERA_REQUEST) {
            cameraImage = (Bitmap) data.getExtras().get("data");
            imgSpecimenPhoto.setImageBitmap(cameraImage);
        }
    }

}

一切正常,除了:

public void btnDeletePhotoClicked(View v) {
    iv = (ImageView) findViewById(R.id.imgSpecimenPhoto);
    iv.setImageDrawable(null);
    Toast.makeText(AddIncome.this, "Photo deleted", Toast.LENGTH_SHORT).show();

}

按下按钮后我无法删除照片 只有Toast“Photo Deleted”才有效。

http://i.stack.imgur.com/BhOaU.jpg

1 个答案:

答案 0 :(得分:1)

将此代码粘贴到onCreate()活动方法中。您更新的onCreate方法应如下所示。

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.adddata); 
    ImageView iv = (ImageView) findViewById(R.id.imgSpecimenPhoto);
    iv.setImageDrawable(null);
}