简体   繁体   中英

Android child view being null within onActivityResult in parents activity

In my Android application there is a view, 'manage players', and within that there is an 'add player' view. On the 'add player' view I am accessing the camera, so I have an override on onActivityResult. When it hits this, within the 'manage players' activity, the 'add player' view is now null. Here is a sample of the code:

public class ManagePlayersActivity {

    private AddPlayerView addPlayerView = null;
    private ManagePlayersView view;
    private Context context;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    context = this;

    view = ManagePlayersViewFactory.create(this);
    setContentView(view.asView());

        public void onEvent(ClickEvent event) {
            final Dialog d = new Dialog(context);
            ManagePlayersActivity.this.addPlayerView = AddPlayerViewFactory.create(context);

            ManagePlayersActivity.this.addPlayerView.setButtonPhotoPickerOnClickListener(new EventListener<ClickEvent>() {
                public void onEvent(ClickEvent event) {

                    ManagePlayersActivity.this.addPlayerView.setPlayerPhoto("Setting Photo");

                    PackageManager pm = context.getPackageManager();

                    if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {

                        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                            startActivityForResult(cameraIntent, CAMERA_REQUEST);
                    }                   
                }

            });

            d.show();
        }
    });
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_REQUEST) {
        if (resultCode == RESULT_OK) {
            ManagePlayersActivity.this.addPlayerView.setPlayerPhoto("Photo set");
        } 
    }
}
}

So, after doing that the addPlayerView is null in the onActivity result. In honesty, I am very new to Android and Java, I am ac# web developer with no experience in MVC, so it could be a misunderstanding of how it fundamentally works. But, any help would be greatly appreciated!

Thanks, Andy

Write this line above the event handler(ie, outside onEvent()):

ManagePlayersActivity.this.addPlayerView = AddPlayerViewFactory.create(context);

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