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.