android - 更改为新 Activity 时 Android Native 崩溃

标签 android android-ndk openfeint cocos2d-x

我的应用程序无法打开 OpenFeint 仪表板方法。 native c++ 库的实现使用 cocos2d-x 作为图形库,但它有一个处理程序和一个包装器以允许使用 OpenFeint 函数。 OpenFeint 初始化和非 Activity 方法正常工作。

当从 Jni 调用或在 Java onCreate 初始化中调用 openLaderBoards 或 openAchievements 等 UI 仪表板函数时,应用程序会崩溃。

编辑:我已经测试过,它发生在我尝试的任何 Activity 更改上,甚至是我自己的新类(class)。

EDIT2:我在一个类似的问题上有 +100 赏金,任何想出答案的人都会得到它。

代码

Activity :

public class App extends Cocos2dxActivity{
private Cocos2dxGLSurfaceView mGLView;

OpenFeintX m_kOpenFeintX;

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

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

    InternetConnection.setM_kActivity(this);

    m_kOpenFeintX = new OpenFeintX( this);      

    setContentView(R.layout.applayout);
    mGLView = (Cocos2dxGLSurfaceView) findViewById(R.id.game_gl_surfaceview);
    mGLView.setTextField((EditText)findViewById(R.id.textField));                  

// Testspace for new Activities, OpenFeint or self-made
//
// Intent myIntent = new Intent(this, TestActivity.class);
// startActivityForResult(myIntent, 0);
// Dashboard.open();


// Cocos2d-x scene opens after this

}

 static {
     System.loadLibrary("TestProject");
     // Native library loaded for cocos2d-x
 }

包装器:

public class OpenFeintX {

private static OpenFeintXHandler ms_kOpenFeintHandler;

public OpenFeintX(Activity kActivity) {
    initializeOpenFeint("TestApp", "derp",
            "hurr", "6546516516541",
            kActivity, ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    ms_kOpenFeintHandler = new OpenFeintXHandler();

}

public static void openLeaderBoards() {     
    Message msg = new Message();
    msg.what = OpenFeintXHandler.SHOW_LEADERBOARDS;
    ms_kOpenFeintHandler.sendMessage(msg);
}

处理程序 openDashboard 函数:

private void openLeaderBoards() {
    System.out.println("Opening Dashboard");
    Dashboard.openLeaderboards();
}

list :

<application
    android:debuggable="true"
    android:label="@string/app_name">
    <activity
        android:configChanges="orientation"
        android:label="@string/app_name"
        android:name=".App"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />

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

    <activity android:name="com.openfeint.internal.ui.IntroFlow"
              android:label=".IntroFlow"
              android:configChanges="orientation|keyboardHidden"
              android:theme="@style/OFNestedWindow" />
    <activity android:name="com.openfeint.api.ui.Dashboard"
              android:label=".Dashboard"
              android:configChanges="orientation|keyboardHidden"
              android:theme="@style/OFNestedWindow"/>
    <activity android:name="com.openfeint.internal.ui.Settings"
              android:label=".Settings"
              android:configChanges="orientation|keyboardHidden"
              android:theme="@style/OFNestedWindow"/>
    <activity android:name="com.openfeint.internal.ui.NativeBrowser"
              android:label=".NativeBrowser"
              android:configChanges="orientation|keyboardHidden"
              android:theme="@style/OFNestedWindow"/>
</application>

Stacktrace(不会在 SO 中缩进):

http://pastebin.com/jsmSbgw4

最佳答案

答案简单但复杂。当应用程序更改为新 Activity 时,将调用 cocos2d-x MessageJNI 中的 nativeOnPause 方法。此方法应该调用 CCApplication::sharedApplication(),但我的一个类之前调用​​了 CCApplication 析构函数,它将共享单例清除为 null。

尽管答案很容易确定并且针对特定项目,但我还是会就我找到它的原因提供一些建议。请记住,我所有的工具都是 Windows 和 Cygwin。

首先,让 Eclipse 在 clean 上为您执行 ndk-builds。

右键单击您的项目 -> 属性 -> C/C++ 构建。构建器设置选项卡,命令

  C:\NVPACK\cygwin\bin\bash.exe -c "cd /cygdrive/path/to/project && ./build_script.sh"

其次,设置调试器。

使用 this tutorial .可能需要一些时间和尝试才能到达那里,但这是值得的。我的 adb-server 调用脚本是

export ANDROID_NDK_ROOT=/cygdrive/c/NVPACK/android-ndk-r6b/
cd $ANDROID_NDK_ROOT
./ndk-gdb-eclipse --adb=/cygdrive/c/NVPACK/android-sdk-windows/platform-tools/adb     --project=/cygdrive/c/project/path --force --verbose

第三,将您的 logcat 设置为详细。

更新:现在可以跳过最后一步,因为最新版本的 Logcat 输出整个堆栈跟踪的类和行。

你会得到像我问题中那样的长堆栈跟踪。要检查堆栈跟踪错误指向何处,请遵循此 other tutorial.我访问图书馆的脚本是

C:\NVPACK\android-ndk-r6b\toolchains\x86-4.4.3\prebuilt\windows\bin\i686-android-linux-addr2line.exe -C -f -e c:\path\to\project\libMyLib.so

关于android - 更改为新 Activity 时 Android Native 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10314789/

相关文章:

android - 前一个 fragment 在新 fragment 下方可见

Android NDK - getFieldID 结果导致 NoSuchFieldError

game-center - 使用 OpenFeint 或 Game Center 有什么优势?

iOS 和 Cocos2d - 更改 CCSprite 的图像和新尺寸 = 失败

java - 如何使用 googletest 测试在 android 上调用 java 的 C++ 代码?

java - OpenFeint 与 cocos2d-x(iOS 和 Android)

android - 尝试查看 Activity 预览时,我在最新的 android studio 中出现渲染错误

java - 运行此代码时出现 ANR 强制关闭

android - Android 中的连接拒绝错误

Android float 学性能