Android应用程序无法在仿真器上运行 - 遵循Lynda教程的Cocos2dx

时间:2013-12-23 09:56:00

标签: android eclipse android-ndk emulation cocos2d-x

有一个lynda教程,称为构建和货币化Android游戏应用程序,我一直在尝试按照他们的方式设置项目。问题是当我将cocos2dx项目添加到eclipse时,由于某种原因我无法在模拟器中运行它。这在网上是一个明显的问题,但我无法弄清楚如何解决它。这就是代码的样子:

package com.gk.moleitx;

import org.cocos2dx.lib.Cocos2dxActivity;
import org.cocos2dx.lib.Cocos2dxEditText;
import org.cocos2dx.lib.Cocos2dxGLSurfaceView;
import org.cocos2dx.lib.Cocos2dxRenderer;

import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ConfigurationInfo;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.FrameLayout;
import android.view.ViewGroup;

public class moleitx extends Cocos2dxActivity{
private Cocos2dxGLSurfaceView mGLView;

protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    if (detectOpenGLES20()) {
        // get the packageName,it's used to set the resource path
        String packageName = getApplication().getPackageName();
        super.setPackageName(packageName);

        // FrameLayout
        ViewGroup.LayoutParams framelayout_params =
            new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
                                       ViewGroup.LayoutParams.FILL_PARENT);
        FrameLayout framelayout = new FrameLayout(this);
        framelayout.setLayoutParams(framelayout_params);

        // Cocos2dxEditText layout
        ViewGroup.LayoutParams edittext_layout_params =
            new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
                                       ViewGroup.LayoutParams.WRAP_CONTENT);
        Cocos2dxEditText edittext = new Cocos2dxEditText(this);
        edittext.setLayoutParams(edittext_layout_params);

        // ...add to FrameLayout
        framelayout.addView(edittext);

        // Cocos2dxGLSurfaceView
        mGLView = new Cocos2dxGLSurfaceView(this);

        // ...add to FrameLayout
        framelayout.addView(mGLView);

        mGLView.setEGLContextClientVersion(2);
        mGLView.setCocos2dxRenderer(new Cocos2dxRenderer());
        mGLView.setTextField(edittext);

        if (Build.FINGERPRINT.startsWith("generic")) {
            mGLView.setEGLConfigChooser(8,8,8,8,16,0);
        }

        // Set framelayout as the content view
        setContentView(framelayout);
    }
    else {
        Log.d("activity", "don't support gles2.0");
        finish();
    }   
}

 @Override
 protected void onPause() {
     super.onPause();
     mGLView.onPause();
 }

 @Override
 protected void onResume() {
     super.onResume();
     mGLView.onResume();
 }

 private boolean detectOpenGLES20() 
 {
     ActivityManager am =
            (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
     ConfigurationInfo info = am.getDeviceConfigurationInfo();
     return (info.reqGlEsVersion >= 0x20000 || Build.FINGERPRINT.startsWith("generic"));
 }

 static {
     System.loadLibrary("game");
 }
}

然后Android Manifest文件如下所示:

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

<uses-sdk android:minSdkVersion="8"
    android:targetSdkVersion="18" />
<uses-feature android:glEsVersion="0x00020000" android:required="true"/>

<application android:label="@string/app_name"
    android:debuggable="true"
    android:icon="@drawable/icon">

    <activity android:name=".moleitx"
              android:label="@string/app_name"
              android:screenOrientation="landscape"
              android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
              android:configChanges="orientation">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
<supports-screens android:largeScreens="true"
                  android:smallScreens="true"
                  android:anyDensity="true"
                  android:normalScreens="true"/>
</manifest> 

项目正确构建,然后在logcat中给我一个错误。

我的LogCat错误目前如下:

 12-23 01:50:52.362: D/dalvikvm(355): Trying to load lib /data/data/com.gk.moleitx    /lib/libgame.so 0x40514e10
12-23 01:50:52.461: D/dalvikvm(355): Added shared lib /data/data/com.gk.moleitx/lib/libgame.so 0x40514e10
12-23 01:50:52.501: W/apk path(355): /data/app/com.gk.moleitx-1.apk
12-23 01:50:52.571: D/AndroidRuntime(355): Shutting down VM
12-23 01:50:52.571: W/dalvikvm(355): threadid=1: thread exiting with uncaught exception (group=0x40015560)
12-23 01:50:52.591: E/AndroidRuntime(355): FATAL EXCEPTION: main
12-23 01:50:52.591: E/AndroidRuntime(355): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gk.moleitx/com.gk.moleitx.moleitx}:         java.lang.IllegalStateException: setRenderer has already been called for this instance.
12-23 01:50:52.591: E/AndroidRuntime(355):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-23 01:50:52.591: E/AndroidRuntime(355):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-23 01:50:52.591: E/AndroidRuntime(355):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-23 01:50:52.591: E/AndroidRuntime(355):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-23 01:50:52.591: E/AndroidRuntime(355):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-23 01:50:52.591: E/AndroidRuntime(355):  at android.os.Looper.loop(Looper.java:123)
12-23 01:50:52.591: E/AndroidRuntime(355):  at android.app.ActivityThread.main(ActivityThread.java:3683)
12-23 01:50:52.591: E/AndroidRuntime(355):  at java.lang.reflect.Method.invokeNative(Native Method)
12-23 01:50:52.591: E/AndroidRuntime(355):  at java.lang.reflect.Method.invoke(Method.java:507)
12-23 01:50:52.591: E/AndroidRuntime(355):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-23 01:50:52.591: E/AndroidRuntime(355):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-23 01:50:52.591: E/AndroidRuntime(355):  at dalvik.system.NativeStart.main(Native Method)
12-23 01:50:52.591: E/AndroidRuntime(355): Caused by: java.lang.IllegalStateException: setRenderer has already been called for this instance.
12-23 01:50:52.591: E/AndroidRuntime(355):  at android.opengl.GLSurfaceView.checkRenderThreadState(GLSurfaceView.java:1614)
12-23 01:50:52.591: E/AndroidRuntime(355):  at android.opengl.GLSurfaceView.setEGLConfigChooser(GLSurfaceView.java:355)
12-23 01:50:52.591: E/AndroidRuntime(355):  at android.opengl.GLSurfaceView.setEGLConfigChooser(GLSurfaceView.java:392)
12-23 01:50:52.591: E/AndroidRuntime(355):  at com.gk.moleitx.moleitx.onCreate(moleitx.java:79)
12-23 01:50:52.591: E/AndroidRuntime(355):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-23 01:50:52.591: E/AndroidRuntime(355):  at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-23 01:50:52.591: E/AndroidRuntime(355):  ... 11 more

我正在使用cocos2d-x 2.0.1和android ndk r8b以及android 2.3.3(10),因为教程说这样做。我也使用Windows 7。

我的模拟器已选择使用主机gpu,我无法弄清楚问题是什么。任何帮助/见解都会很棒,因为我在线查看了所有内容。我对此非常陌生,所以它让它变得更难。感谢

更新:新错误:

12-23 15:03:27.574: D/dalvikvm(593): Trying to load lib /data/data/com.gk.moleitx/lib/libgame.so 0x40513408
12-23 15:03:27.773: D/dalvikvm(593): Added shared lib /data/data/com.gk.moleitx/lib/libgame.so 0x40513408
12-23 15:03:27.993: W/apk path(593): /data/app/com.gk.moleitx-2.apk
12-23 15:03:28.465: D/libEGL(593): egl.cfg not found, using default config
12-23 15:03:28.473: D/libEGL(593): loaded /system/lib/egl/libGLES_android.so
12-23 15:03:28.484: W/dalvikvm(593): threadid=11: thread exiting with uncaught exception (group=0x40015560)
12-23 15:03:28.652: E/AndroidRuntime(593): FATAL EXCEPTION: GLThread 12
12-23 15:03:28.652: E/AndroidRuntime(593): java.lang.IllegalArgumentException: No configs match configSpec
12-23 15:03:28.652: E/AndroidRuntime(593):  at android.opengl.GLSurfaceView$BaseConfigChooser.chooseConfig(GLSurfaceView.java:763)
12-23 15:03:28.652: E/AndroidRuntime(593):  at android.opengl.GLSurfaceView$EglHelper.start(GLSurfaceView.java:919)
12-23 15:03:28.652: E/AndroidRuntime(593):  at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1264)
12-23 15:03:28.652: E/AndroidRuntime(593):  at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1118)

我不知道我做了什么,但它现在在模拟器中有效!最后!!

3 个答案:

答案 0 :(得分:1)

我遇到了类似的问题。确保虚拟设备上的API至少是您下载的cocos2dx所需的API。我继续下载最新的(2.2.2),它需要API 17.如果您的设备没有最低API,它将无法运行。如果我没有弄错的话,那个Lynda教程需要API 10或更高版本,但我可能会弄错。查看您的虚拟设备,看看它是否有所作为。使用具有更高API的测试并查看它是否有所不同并不太难。祝你好运!

答案 1 :(得分:0)

mGLView.setCocos2dxRenderer(new Cocos2dxRenderer());
mGLView.setTextField(edittext);

if (Build.FINGERPRINT.startsWith("generic")) {
    mGLView.setEGLConfigChooser(8,8,8,8,16,0);
}

setEGLConfigChooser()只能在setRenderer()之前调用。在setEGLConfigChooser()来电之前移动setCocos2dxRenderer()


要解决java.lang.IllegalArgumentException: No configs match configSpec问题,请确保您的模拟器已启用 GPU模拟。或者只使用真实设备而不是模拟器。

答案 2 :(得分:0)

并在andoridemainfest.xml更改

    android:icon="@drawable/icon">

    android:icon="@drawable/ic_launcher">