phoneGap 本机应用程序中的 Android 手机旋转问题

发布于 2024-12-28 02:08:23 字数 251 浏览 2 评论 0原文

我对 Java android 开发很陌生。所以我的应用程序有问题。

我正在使用phoneGap开源和Jquery移动框架构建一个Android应用程序。 正常工作时看起来不错。

但问题是......当我在运行应用程序后尝试旋转手机时,应用程序意外关闭。

这不仅适用于我的手机,它也发生在每部 Android 手机上。

我真的很想删除应用程序上的屏幕旋转。

希望你能理解

请帮助我......

I'm quite new in Java android development. So I have an issue with my app.

I was build a android app with phoneGap opensource and Jquery mobile framework.
That's looking good normally working.

But the issue is.... when I'm trying to rotate the phone after running the app, then unexpectedly the app is closing.

It's not only for my phone it's happening on every android phone.

I really want to remove the screen rotation on the app.

Hope you understand

Help me please....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

ま昔日黯然 2025-01-04 02:08:23

我可能遇到了同样的问题 - 旋转时崩溃。我认为我没有直接从教程中复制 AndroidManifest.xml。我没有添加

 android:configChanges="orientation|keyboardHidden" 

到第一个活动中。请参阅我的前后对比:

之后(工作):

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".App"
        android:label="@string/app_name"  android:configChanges="orientation|keyboardHidden">
        <intent-filter>

之前(旋转时崩溃):

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".App"
        android:label="@string/app_name">
        <intent-filter>

不确定您是否真的想修复屏幕或阻止其崩溃,但如果您想修复纵向或横向,您可以执行以下操作:

    <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".App"
        android:label="@string/app_name"  android:configChanges="orientation|keyboardHidden"
        android:screenOrientation="landscape" >
        <intent-filter>

I had probably the same issue - crash on rotate. I dont think I had the AndroidManifest.xml copied right from the tutorial. I didnt add

 android:configChanges="orientation|keyboardHidden" 

to the 1st activity. See my after and before:

After (working):

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".App"
        android:label="@string/app_name"  android:configChanges="orientation|keyboardHidden">
        <intent-filter>

Before (crash on rotate):

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".App"
        android:label="@string/app_name">
        <intent-filter>

Not sure if you really want to fix the screen or stop it crashing but if you want to fix portrait or landscape you can do this:

    <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".App"
        android:label="@string/app_name"  android:configChanges="orientation|keyboardHidden"
        android:screenOrientation="landscape" >
        <intent-filter>
一紙繁鸢 2025-01-04 02:08:23

您必须在主页中使用以下 js。

$(document).ready(function() {
    var canDetect = "onorientationchange" in window;
    var orientationTimer = 0;

    var ROTATION_CLASSES = {
        "0": "none",
        "90": "right",
        "-90": "left",
        "180": "flipped"
    };

    $(window).bind(canDetect ? "orientationchange" : "resize", function(evt) {
        clearTimeout(orientationTimer);
        orientationTimer = setTimeout(function() {
            // display the event type and window details
            $("#event-type").html(evt.type);
            $("#window-orientation").html(window.orientation);
            $("#window-width").html(window.innerWidth);
            $("#window-height").html(window.innerHeight);

            // given we can only really rely on width and height at this stage, 
            // calculate the orientation based on aspect ratio
            var aspectRatio = 1;
            if (window.innerHeight !== 0) {
                aspectRatio = window.innerWidth / window.innerHeight;
            } // if

            // determine the orientation based on aspect ratio
            var orientation = aspectRatio <= 1 ? "portrait" : "landscape";

            // if the event type is an orientation change event, we can rely on
            // the orientation angle
            var rotationText = null;
            if (evt.type == "orientationchange") {
                rotationText = ROTATION_CLASSES[window.orientation.toString()];
            } // if

            $(window).trigger("reorient", [orientation, rotationText]);
        }, 500);
    });

    $(window).bind("reorient", function(evt, orientation, rotation) {
        // display the details we have determine from the display
        $("#orientation").html(orientation);
        $("#rotation-class").html(rotation);
    });
});

这对我有用,无需在 manifest.xml 中执行任何操作

You have to use the following js in your main page.

$(document).ready(function() {
    var canDetect = "onorientationchange" in window;
    var orientationTimer = 0;

    var ROTATION_CLASSES = {
        "0": "none",
        "90": "right",
        "-90": "left",
        "180": "flipped"
    };

    $(window).bind(canDetect ? "orientationchange" : "resize", function(evt) {
        clearTimeout(orientationTimer);
        orientationTimer = setTimeout(function() {
            // display the event type and window details
            $("#event-type").html(evt.type);
            $("#window-orientation").html(window.orientation);
            $("#window-width").html(window.innerWidth);
            $("#window-height").html(window.innerHeight);

            // given we can only really rely on width and height at this stage, 
            // calculate the orientation based on aspect ratio
            var aspectRatio = 1;
            if (window.innerHeight !== 0) {
                aspectRatio = window.innerWidth / window.innerHeight;
            } // if

            // determine the orientation based on aspect ratio
            var orientation = aspectRatio <= 1 ? "portrait" : "landscape";

            // if the event type is an orientation change event, we can rely on
            // the orientation angle
            var rotationText = null;
            if (evt.type == "orientationchange") {
                rotationText = ROTATION_CLASSES[window.orientation.toString()];
            } // if

            $(window).trigger("reorient", [orientation, rotationText]);
        }, 500);
    });

    $(window).bind("reorient", function(evt, orientation, rotation) {
        // display the details we have determine from the display
        $("#orientation").html(orientation);
        $("#rotation-class").html(rotation);
    });
});

That works for me without doing anything in the manifest.xml

淡水深流 2025-01-04 02:08:23

我也有同样的问题。我正在使用 android:minSdkVersion="8"android:targetSdkVersion="16" 开发 PhoneGap 应用程序。当我尝试将 screenSize 添加到 android:configChanges 时,它说 screenSize 不存在。然后我减少了 targetSdkVersion ,一切都开始完美运行。这是我的清单文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:windowSoftInputMode="adjustPan"
  package="com.planetclub.mobile" android:versionName="1.1" android:versionCode="6">
<supports-screens
    android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:resizeable="true"
    android:anyDensity="true"
    />
<uses-permission android:name="android.permission.INTERNET" />   
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application android:icon="@drawable/planet" android:label="@string/app_name" >
    <activity android:name=".Main" 
        android:label="@string/app_name" 
        android:configChanges="orientation|keyboardHidden|locale" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8"/>

I had the same problem. I was developing PhoneGap application with android:minSdkVersion="8" and android:targetSdkVersion="16". When I tried to add screenSize to android:configChanges it says that screenSize does not exist. Then I reduced the targetSdkVersion and everything starts working perfectly. This is my manifest file

<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:windowSoftInputMode="adjustPan"
  package="com.planetclub.mobile" android:versionName="1.1" android:versionCode="6">
<supports-screens
    android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:resizeable="true"
    android:anyDensity="true"
    />
<uses-permission android:name="android.permission.INTERNET" />   
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application android:icon="@drawable/planet" android:label="@string/app_name" >
    <activity android:name=".Main" 
        android:label="@string/app_name" 
        android:configChanges="orientation|keyboardHidden|locale" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8"/>

贩梦商人 2025-01-04 02:08:23

我认为你的问题与任何特定框架无关;您很可能引用了一些无效的实例。请记住,方向更改会导致系统经历以下过程:保存实例状态、暂停、停止、销毁,然后使用保存的状态创建活动的新实例。因此,例如,如果您保存对某个实例的引用,那么在方向更改后,该引用将不再有效(可能为空)。

您应该尝试修复它,请查看此参考。但是,如果您确实不想允许屏幕方向更改,请将以下属性添加到清单文件中的主要活动中:

android:screenOrientation="portrait/landscape"
android:configChanges="keyboardHidden|orientation|screenSize">

I think your problem has nothing to do with any specific framework; most likely you reference some invalid instance. Bear in mind that an orientation change causes the system to go through the process of saving instance state, pausing, stopping, destroying, and then creating a new instance of the activity with the saved state. So if you - for example, save a reference to some instance, then after an orientation change the reference is not valid any more (probably null).

You should try to fix it, check this reference. But if you really don't want to allow screen orientation changes, then add following property to your main activity in the manifest file:

android:screenOrientation="portrait/landscape"
android:configChanges="keyboardHidden|orientation|screenSize">
孤独患者 2025-01-04 02:08:23

如果您正在针对 Android API 13 或更高版本进行开发,则需要将 screenSize 添加到 android:configChanges 以防止崩溃。像这样:

android:configChanges="orientation|screenSize|keyboardHidden"

在这里找到这个:

https://groups.google.com/forum/?fromgroups#!msg/phonegap/HYUfLJF1oH0/r9rZTfz_cccJ

If you are developing for Android API 13 or higher you need to add screenSize to android:configChanges in order to prevent a crash. Like this:

android:configChanges="orientation|screenSize|keyboardHidden"

Found this here:

https://groups.google.com/forum/?fromgroups#!msg/phonegap/HYUfLJF1oH0/r9rZTfz_cccJ

北斗星光 2025-01-04 02:08:23

我遇到了同样的问题。当我从纵向切换到横向或从横向切换到纵向时,该应用程序死机了。没有理由。

我检查了 android:configChanges="orientation|keyboardHidden" 并添加了屏幕尺寸,但没有更改。
我做了很多改变。我最终取消了该行并重写了它,它成功了。
我认为该行存在 Eclipse 编辑器未发现且不可见的问题。原始行是从phonegap 安装中复制/粘贴的。

I ran though the same problem. The app died when I moved from portrait to landscape or landscape to portrait. No reason.

I checked the android:configChanges="orientation|keyboardHidden" and added the screensize but no change.
I made so many changes. I finally cancelled the line and rewrote it and it worked.
I think the line had a problem not seen by the Eclipse editor and not visible. The original line was a copy/paste from the phonegap installation.

方圜几里 2025-01-04 02:08:23

我遇到了同样的问题。但我的 android:configChanges 设置正确。最后我发现我需要将miniSdkVersion从15更改为7。

I met the same problem. But my android:configChanges was set correctly. In the end I found that I need to change the miniSdkVersion from 15 to 7.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文