简体   繁体   中英

Why does alertDialog cause my app to crash?

public class SignUpActivity extends AppCompatActivity {

private CircleImageView profilePic,galleryPick,cameraPick;
private ActivityResultLauncher<Intent> activityResultLauncher;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sign_up);

    profilePic = findViewById(R.id.circular_image);

    
    activityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
        @Override
        public void onActivityResult(ActivityResult result)
        {
            if(result.getResultCode() == RESULT_OK && result.getData() != null)
            {
                Bundle bundle = result.getData().getExtras();
                Bitmap bitmap = (Bitmap) bundle.get("data");
                profilePic.setImageBitmap(bitmap);
            }
        }
    });

    profilePic.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
           chooseProfilePic();
        }
    });

}


private void chooseProfilePic()
{
    AlertDialog.Builder builder = new AlertDialog.Builder(SignUpActivity.this);
    LayoutInflater inflater = getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.alert_dialog,null);
    builder.setCancelable(false);
    builder.setView(dialogView);

    galleryPick = findViewById(R.id.gallery_pick);
    cameraPick = findViewById(R.id.camera_pick);

    AlertDialog alertDialog = builder.create();
    alertDialog.show();



    cameraPick.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
            Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            if(cameraIntent.resolveActivity(getPackageManager()) != null)
            {
                activityResultLauncher.launch(cameraIntent);
            }
        }
    });
}

}

So I am a beginner in Android Studio and since startForActivityResult is deprecated I am looking for something to replace it. The new method works but there occurs a problem when used with private method including the alert dialog.

You are finding the resourceId of the button in the dialog from wrong view. Buttons are present in dialog while you are finding it from activity/fragment view.
Update your code to find it from dialogView.

 AlertDialog.Builder builder = new AlertDialog.Builder(SignUpActivity.this);
    LayoutInflater inflater = getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.alert_dialog,null);
    builder.setCancelable(false);
    builder.setView(dialogView);

    galleryPick = dialogView.findViewById(R.id.gallery_pick); // find id from dialog view
    cameraPick = dialogView.findViewById(R.id.camera_pick); // find id from dialog view

    AlertDialog alertDialog = builder.create();
    alertDialog.show();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM