In an expandable recyclerivew, when it expands to show the son, in the son I have a camera button, I try to take a photo and save it in the variable but I have problems because I do not get to the public void method onActivityResult (int requestCode , int resultCode, Intent data), since the child class extends from chilviewholder and I can not invoke that method since I need to extend from AppCompatActivity, This is the mistake he gave me
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.PackageManager android.app.Activity.getPackageManager () 'on a null object reference
The following is my code:
public class ChildQuestionBooleanViewHolder extends ChildViewHolder {
public RadioButton booleanYes, booleanNo;
public Button btn_photo_boolean, btn_galery_boolean;
public EditText txtarea_boolean;
static final int REQUEST_IMAGE_CAPTURE = 1;
public static final int MY_PERMISSIONS_REQUEST_CAMERA = 100;
private static final int MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 112;
Activity activity;
private ImageView imagen;
public ChildQuestionBooleanViewHolder(View itemView, Preguntas pregunta, final Activity activity) {
super(itemView);
this.activity=activity;
booleanYes = (RadioButton) itemView.findViewById(R.id.booleanYes);
booleanNo = (RadioButton) itemView.findViewById(R.id.booleanNo);
btn_photo_boolean = (Button)
itemView.findViewById(R.id.btn_photo_boolean);
btn_galery_boolean = (Button) itemView.findViewById(R.id.btn_galery_boolean);
txtarea_boolean = (EditText) itemView.findViewById(R.id.txtarea_boolean);
booleanYes.setText(pregunta.respuestas.get(0).vAnswer);
booleanNo.setText(pregunta.respuestas.get(1).vAnswer);
if (Boolean.parseBoolean(pregunta.getMgetQuestions().bImage)){
btn_photo_boolean.setVisibility(View.VISIBLE);
}
if (Boolean.parseBoolean(pregunta.getMgetQuestions().bComments)){
txtarea_boolean.setVisibility(View.VISIBLE);
}
booleanYes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
booleanNo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
btn_photo_boolean.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
llamarIntentCamara();
}
});
btn_galery_boolean.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
private void llamarIntentCamara(){
foto foto = new foto();
foto.llamarIntentCamara2();
}
public class foto extends AppCompatActivity {
int permissionCheck = ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
int permissionCheck2 = ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA);
@Override
public void onCreate( Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
if (ContextCompat.checkSelfPermission(activity,
Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(activity,
Manifest.permission.CAMERA)) {
} else {
ActivityCompat.requestPermissions(activity,
new String[]{Manifest.permission.CAMERA},
MY_PERMISSIONS_REQUEST_CAMERA);
}
}
if (ContextCompat.checkSelfPermission(activity,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(activity,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
} else {
ActivityCompat.requestPermissions(activity,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
}
}
}
private void llamarIntentCamara2(){
//File miFile = new File(Environment.getExternalStorageDirectory(), DIRECTORIO_IMAGEN);
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(activity.getPackageManager()) != null) { // En esta linea me da error en el getPackageManager
activity.startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
Log.e("IMAGEN", String.valueOf(requestCode));
Log.e("entro aqui", "ActivityResult");
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
imagen.setImageBitmap(imageBitmap);
Log.e("GUARDO LA IMAGEN AQUI", String.valueOf(imagen));
}
}
@Override
public void onRequestPermissionsResult(int requestCode,String permissions[], int[] grantResults) {
if (MY_PERMISSIONS_REQUEST_CAMERA == 100) {
Log.e("entro aqui", "Entro a Camara");
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
}
// other 'case' lines to check for other
// permissions this app might request
if (MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE == 112){
Log.e("entro aqui", "External storage");
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
}
}
}
}
Inside my ChildquestionBooleanViewHolder class I put in another class called a picture that if it extends from AppCompatActivity but I do not know if it's doing it, or send it to call from the main activity that is Questions, then there's the adapter, and then the class Childquestion ...