我有一个使用libgdx框架构建的功能完备的java应用程序,可以在桌面上部署。但是,我想从中获取代码并创建Android应用程序。
我创建了Android项目,将代码重新路由到Android项目的源文件夹,添加了Android特定主要方法,并编辑了清单xml文件以使用主要活动 - 在本例中为“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
以下是我的清单:
<?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>
以下是我的主要活动类“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。任何和所有建议将不胜感激!
答案 0 :(得分:3)
更新的例外可识别问题:
Caused by: java.lang.SecurityException: Neither user 10048 nor current process has android.permission.WAKE_LOCK.
您只需将其添加到清单中即可:
<uses-permission android:name="android.permission.WAKE_LOCK"/>
请参阅:https://code.google.com/p/libgdx/wiki/ApplicationConfiguration#Permissions
或者,如果您不打算使用唤醒锁定,请从config.useWakelock = true;
RoadRusherActivity
更新:如下面的评论所述,如果您只想保持屏幕,请使用WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
而不是唤醒锁。将其添加到您的onCreate
方法:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);