简体   繁体   English

为什么屏幕方向锁定不起作用

[英]Why screen Orientation lock will not work

I am trying to fix screen orientation to portrait for one page in my ionic 4 application.我正在尝试将 ionic 4 应用程序中一页的屏幕方向修复为纵向。

using the cordova plugin: https://github.com/apache/cordova-plugin-screen-orientation使用cordova插件: 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:当应用程序尝试锁定页面时,我在 android studio 上收到以下错误消息:

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下面是我的 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下面是我想锁定方向的页面,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() {
     console.log("lockorientation  ");
     screen.orientation.lock('portrait');
     console.log("lockorientation  ");
   }

I was also playing around with the confix.xml file to lock the application in portrait, using:我还在使用 confix.xml 文件以纵向锁定应用程序,使用:

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

But the application just ignores the command and continues to rotate.但应用程序只是忽略该命令并继续旋转。

在您的代码中使用它

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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