java - 将 libgdx 中的桌面应用程序转换为 android 的步骤是什么?

标签 java android libgdx

我有一个功能齐全的 Java 应用程序,它是使用部署在桌面上的 libgdx 框架构建的。但是,我想从中获取代码并创建 Android 应用程序。

我已经创建了 Android 项目,将代码重新路由到 Android 项目的源文件夹,添加了 Android 特定的主要方法,并编辑了 list xml 文件以使用主要 Activity - 在本例中为“RoadRusherActivity”。但是,我遇到了以下错误:

04-28 15:44:41.195: E/Trace(2452): error opening trace file: No such file or directory (2)
04-28 15:44:41.355: D/dalvikvm(2452): Trying to load lib /data/app-lib/com.me.mygdxgame-2/libgdx.so 0x40ce58c8
04-28 15:44:41.385: D/dalvikvm(2452): Added shared lib /data/app-lib/com.me.mygdxgame-2/libgdx.so 0x40ce58c8
04-28 15:44:41.415: D/dalvikvm(2452): No JNI_OnLoad found in /data/app-lib/com.me.mygdxgame-2/libgdx.so 0x40ce58c8, skipping init
04-28 15:44:41.465: D/libEGL(2452): Emulator without GPU support detected. Fallback to software renderer.
04-28 15:44:41.485: D/libEGL(2452): loaded /system/lib/egl/libGLES_android.so
04-28 15:44:41.675: D/AndroidRuntime(2452): Shutting down VM
04-28 15:44:41.675: W/dalvikvm(2452): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-28 15:44:41.685: E/AndroidRuntime(2452): FATAL EXCEPTION: main
04-28 15:44:41.685: E/AndroidRuntime(2452): java.lang.RuntimeException: Unable to resume activity {com.me.mygdxgame/com.me.mygdxgame.RoadRusherActivity}: java.lang.SecurityException: Neither user 10048 nor current process has android.permission.WAKE_LOCK.
04-28 15:44:41.685: E/AndroidRuntime(2452):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2742)
04-28 15:44:41.685: E/AndroidRuntime(2452):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2771)
04-28 15:44:41.685: E/AndroidRuntime(2452):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2235)
04-28 15:44:41.685: E/AndroidRuntime(2452):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-28 15:44:41.685: E/AndroidRuntime(2452):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-28 15:44:41.685: E/AndroidRuntime(2452):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-28 15:44:41.685: E/AndroidRuntime(2452):     at android.os.Looper.loop(Looper.java:137)
04-28 15:44:41.685: E/AndroidRuntime(2452):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-28 15:44:41.685: E/AndroidRuntime(2452):     at java.lang.reflect.Method.invokeNative(Native Method)
04-28 15:44:41.685: E/AndroidRuntime(2452):     at java.lang.reflect.Method.invoke(Method.java:511)
04-28 15:44:41.685: E/AndroidRuntime(2452):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-28 15:44:41.685: E/AndroidRuntime(2452):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-28 15:44:41.685: E/AndroidRuntime(2452):     at dalvik.system.NativeStart.main(Native Method)
04-28 15:44:41.685: E/AndroidRuntime(2452): Caused by: java.lang.SecurityException: Neither user 10048 nor current process has android.permission.WAKE_LOCK.
04-28 15:44:41.685: E/AndroidRuntime(2452):     at android.os.Parcel.readException(Parcel.java:1425)
04-28 15:44:41.685: E/AndroidRuntime(2452):     at android.os.Parcel.readException(Parcel.java:1379)
04-28 15:44:41.685: E/AndroidRuntime(2452):     at android.os.IPowerManager$Stub$Proxy.acquireWakeLock(IPowerManager.java:271)
04-28 15:44:41.685: E/AndroidRuntime(2452):     at android.os.PowerManager$WakeLock.acquireLocked(PowerManager.java:717)
04-28 15:44:41.685: E/AndroidRuntime(2452):     at android.os.PowerManager$WakeLock.acquire(PowerManager.java:686)
04-28 15:44:41.685: E/AndroidRuntime(2452):     at com.badlogic.gdx.backends.android.AndroidApplication.onResume(AndroidApplication.java:241)
04-28 15:44:41.685: E/AndroidRuntime(2452):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1185)
04-28 15:44:41.685: E/AndroidRuntime(2452):     at android.app.Activity.performResume(Activity.java:5182)
04-28 15:44:41.685: E/AndroidRuntime(2452):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2732)
04-28 15:44:41.685: E/AndroidRuntime(2452):     ... 12 more
04-28 15:44:47.705: I/Process(2452): Sending signal. PID: 2452 SIG: 9

以下是我的 list :

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.me.mygdxgame"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".RoadRusherActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/app_name"
            android:screenOrientation="landscape"
            android:theme="@style/FullscreenTheme" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

以下是我的主要 Activity 类,名为“RoadRusherActivity”。

package com.me.mygdxgame;
import android.os.Bundle;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
public class RoadRusherActivity extends AndroidApplication {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
    config.useAccelerometer = false;
    config.useCompass = false;
    config.useWakelock = true;
    config.useGL20 = true;
    initialize(new SimpleBike(), config);
}
}

我只是对如何移植到 Android 感到困惑。任何和所有建议将不胜感激!

最佳答案

更新后的异常确定了问题:

Caused by: java.lang.SecurityException: Neither user 10048 nor current process has android.permission.WAKE_LOCK.

你只需要将它添加到你的 list 中:

<uses-permission android:name="android.permission.WAKE_LOCK"/>

参见:https://code.google.com/p/libgdx/wiki/ApplicationConfiguration#Permissions

或者,如果您不打算使用唤醒锁定,请从 RoadRusherActivity

中删除行 config.useWakelock = true;

更新:如以下评论所述,如果您只是想保持屏幕开启,您应该使用 WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON 而不是唤醒锁。将此添加到您的 onCreate 方法:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

关于java - 将 libgdx 中的桌面应用程序转换为 android 的步骤是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16744215/

相关文章:

libgdx - 在 libgdx 中是否可以呈现 HTML(或者,创建帮助屏幕的最佳实践是什么)?

java - 为什么 Java 按钮使用字符串命令而不是枚举?

android - 如何在 LibGDX 中检查互联网连接

java - 为什么可变字符串会导致安全问题?

android - Android 应用程序中的 Facebook SDK

android - 与另一个 fragment 通信的 fragment 接口(interface)

java - 将用户在 TimePicker 中选择的所有值放入列表中并读取它

android - Box2d坐标系

java - Spring - 无法执行 CommandLineRunner : @Bean

java - 丰富 :effect not working when a rich:panelMenu element is used elsewhere in the page