简体   繁体   中英

Open Activity with Fragment Manger correctly from another activity

I have the following source code :

    private void initToolbars() {
    Toolbar toolbarBottom = (Toolbar) findViewById(R.id.toolbar_bottom);
    if (toolbarBottom != null) {
        toolbarBottom.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.takePhoto:
                        Intent takePhotoIntent = new Intent(NewDocuActivity.this, TakePhotoActivity.class);
                        startActivity(takePhotoIntent);
                }
                return true;
            }
        });
        // Inflate a menu to be displayed in the toolbar
        toolbarBottom.inflateMenu(R.menu.newdocu_toolbar);
    }
}

The OnCreate method of the TakePhotoActivity has this source code :

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

    if (null == savedInstanceState) {
        getFragmentManager().beginTransaction()
                .replace(R.id.container, Camera2BasicFragment.newInstance())
                .commit();
    }
}

If I run the app the error occurs :

Unable to start activity ComponentInfo{com.example.test/com.example.test.TakePhotoActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

The R.id.container is only a FrameLayout. If I run the TakePhotoActivity only in a app it works.

What is the right way to open the TakePhotoActivity from the method initToolBars ?

Do you call (item).setOnClickListener(...) in Camera2BasicFragment.java line 414? looks like the (item) is null.

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