Ionic 4 application - screen orientation lock not working

I am trying to fix screen orientation to portrait for one page in my ionic 4 application.

using the cordova plugin: https://github.com/apache/cordova-plugin-screen-orientation

logging the current orientation and monitoring changes in orientation works fine. But when I try to lock screen orientation nothing happens and the screen continues to rotate when orientation is changed.

I am getting the following error message on android studio when the application attempts to lock the page:

 2020-01-06 13:30:26.003 1913-2198/com.projectX E/Capacitor: Post message error:
 java.lang.NullPointerException: Attempt to invoke virtual method 'void 
 org.apache.cordova.CordovaPlugin.privateInitialize(java.lang.String, 
 org.apache.cordova.CordovaInterface, org.apache.cordova.CordovaWebView, 
 org.apache.cordova.CordovaPreferences)' on a null object reference
     at org.apache.cordova.PluginManager.getPlugin(PluginManager.java:171)
     at org.apache.cordova.PluginManager.exec(PluginManager.java:122)
     at 
     com.getcapacitor.MessageHandler.callCordovaPluginMethod(MessageHandler.java:70)
     at com.getcapacitor.MessageHandler.postMessage(MessageHandler.java:46)
     at android.os.MessageQueue.nativePollOnce(Native Method)
     at android.os.MessageQueue.next(MessageQueue.java:326)
     at android.os.Looper.loop(Looper.java:165)
     at android.os.HandlerThread.run(HandlerThread.java:65)

Below is my app.module.ts

 import { ScreenOrientation } from '@ionic-native/screen-orientation/ngx';
  @NgModule({
    providers: [  
      ScreenOrientation    
    ],
  })

Below is the page I wish to lock orientation, map.page.ts

 import { ScreenOrientation } from '@ionic-native/screen-orientation/ngx';

  export class MapPage {

    places: Place[];
    @ViewChild(IonContent) content: IonContent;

    constructor(public platform: Platform, public renderer: Renderer, private 
       alertCtrl: AlertController, private router: Router, private placeService: 
       PlaceService, public navCtrl: NavController, private screenOrientation: 
       ScreenOrientation) {      
    }

    ngOnInit() {
      screen.orientation.lock('portrait');
    }

I have also tried this line:

 this.screenOrientation.lock(this.screenOrientation.ORIENTATIONS.PORTRAIT);

In what I am sure is related to this problem. I was also playing around with the confix.xml file to lock the application in portrait, using:

 <preference name="orientation" value="portrait" />

But the application just ignores the command and continues to rotate. I am at a loss here, any advice on how I can trouble shoot this further would be really appreciated.

look, in theory what you have there as a code should work since I use the same thing for my page and to block the screen I use this.screenOrientation.lock (this.screenOrientation.ORIENTATIONS.PORTRAIT);
the issue that causes me noise is why you also have the screen orientation lock in the xml (I know it can be done in the xml file but it blocks everything forever). if what you want is to block, for example, only the first screen and unlock the others, remove the blocking of the screens from the xml and manually block them with the blocking commands: this.screenOrientation.lock (this.screenOrientation.ORIENTATIONS.PORTRAIT);
and the unlock command: this.screenOrientation.unlock ();

this command:

<preference name="orientation" value="portrait" />

block all windows in your phone, delete this line and blocked screeen manually.

Hey thanks for the reply, I have used both these methods independently. with neither working. Ideally I wanted to just lock the one screen. But as that was not working I then tried to lock the entire application. Which also did not work. :frowning:

You could place the following:

ngOnInit() {
      this.screenOrientation.lock (this.screenOrientation.ORIENTATIONS.PORTRAIT);
    }

If it gives you an error there let me know to see what the error says.the rest of the code in the modules I see them well

I have tried this, producing the same error
java.lang.NullPointerException…