gpt4 book ai didi

android - 使用 android 和 sdl 从 c++ 进行 java 回调

转载 作者:行者123 更新时间:2023-11-30 02:30:08 27 4
gpt4 key购买 nike

我正在尝试使用 android/SDL c++ 代码调用 java 方法来弹出或更新 Toast。但我越来越

W/dalvikvm( 4026): Invalid indirect reference 0x41e6b710 in decodeIndirectRef

从 Java 端(例如,从一个定期调用的处理程序)更新时,Toast 似乎可以工作,并且我的消息字符串将 Log() OK,但是只要在 setText 中设置任何内容(甚至是“Hello World”) () 我崩溃了。

我的main.cc(引用https://wiki.libsdl.org/SDL_AndroidGetJNIEnv)

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#include "SDL.h"

#include <android/log.h>
#include <jni.h>

#define log(...) __android_log_print(ANDROID_LOG_INFO, "SDL/c", __VA_ARGS__);

void progressMessage(const char *message)
{
log("0) '%s'", message);
JNIEnv *jni_env = (JNIEnv*)SDL_AndroidGetJNIEnv();
log("1) env %08x", (unsigned int)jni_env);
jobject jni_activity = (jobject)SDL_AndroidGetActivity();
log("2) act %08x", (unsigned int)jni_activity);

jclass jni_class(jni_env->GetObjectClass(jni_activity));
log("3) cls %08x", (unsigned int)jni_class);
jstring jstr = jni_env->NewStringUTF(message);
log("4) str %08x", (unsigned int)jstr);
jmethodID method = jni_env->GetMethodID(jni_class,
"progressMessage", "(Ljava/lang/String;)V ");
log("5) met %08x", (unsigned int)method);

jni_env->CallVoidMethod(jni_activity, method, jstr);
log("6) done");
jni_env->DeleteLocalRef(jni_activity);
jni_env->DeleteLocalRef(jni_class);
}

int main(int argc, char *argv[])
{
SDL_Window *window;
SDL_Renderer *renderer;

if(SDL_CreateWindowAndRenderer(0, 0, 0, &window, &renderer) < 0)
exit(2);

Uint8 done = 0;
SDL_Event event;

int loop = 0;
while(!done)
{
if (loop % 100 == 0)
{
char message[256];
sprintf(message, "Log %d", loop);
progressMessage(message);
}

while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT || event.type == SDL_KEYDOWN || event.type == SDL_FINGERDOWN)
{
done = 1;
}
}

int col = loop % 512;
if (col > 255)
col = 256-col;
SDL_SetRenderDrawColor(renderer, col,col,col, 0xFF);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
SDL_Delay(10);

loop++;
}

exit(0);
}

我的类(class)

package com.example.test;

import android.util.Log;
import android.widget.Toast;
import android.os.Bundle;

import org.libsdl.app.SDLActivity;

public class SDLTest extends SDLActivity {

public static Toast toast = null;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);

this.progressMessage("Starting");
}

public void progressMessage(String message) {
Log.i("SDL/java", message);
toast.setText(message);
toast.setDuration(Toast.LENGTH_SHORT);
toast.show();
}
}

这是 logcat 输出

I/SDL     ( 4561): SDL_Android_Init()
I/SDL ( 4561): SDL_Android_Init() finished!
I/SDL/c ( 4561): 0) 'Log 0'
I/SDL/c ( 4561): 1) env 76160ec8
I/SDL/c ( 4561): 2) act 2d900009
I/SDL/c ( 4561): 3) cls 1dd00011
I/SDL/c ( 4561): 4) str 1d200015
I/SDL/c ( 4561): 5) met 6d788688
I/SDL/java( 4561): Log 0
I/SDL/c ( 4561): 6) done
I/ActivityManager( 594): Displayed com.example.test/.SDLTest: +218ms
V/SDL ( 4561): onWindowFocusChanged(): true
I/SDL/c ( 4561): 0) 'Log 100'
I/SDL/c ( 4561): 1) env 76160ec8
I/SDL/c ( 4561): 2) act 1de00011
I/SDL/c ( 4561): 3) cls 2da00009
I/SDL/c ( 4561): 4) str 1dc00019
I/SDL/c ( 4561): 5) met 6d788688
I/SDL/java( 4561): Log 100
I/SDL/c ( 4561): 6) done
I/SDL/c ( 4561): 0) 'Log 200'
I/SDL/c ( 4561): 1) env 76160ec8
I/SDL/c ( 4561): 2) act 41e64a80
W/dalvikvm( 4561): Invalid indirect reference 0x41e64a80 in decodeIndirectRef
I/dalvikvm( 4561): "SDLThread" prio=5 tid=11 RUNNABLE
I/dalvikvm( 4561): | group="main" sCount=0 dsCount=0 obj=0x41e75fd0 self=0x761607a0
I/dalvikvm( 4561): | sysTid=4579 nice=0 sched=0/0 cgrp=apps handle=1981156344
I/dalvikvm( 4561): | state=R schedstat=( 0 0 0 ) utm=3 stm=11 core=1
I/dalvikvm( 4561): at org.libsdl.app.SDLActivity.nativeInit(Native Method)
I/dalvikvm( 4561): at org.libsdl.app.SDLMain.run(SDLActivity.java:915)
I/dalvikvm( 4561): at java.lang.Thread.run(Thread.java:841)
I/dalvikvm( 4561):
E/dalvikvm( 4561): VM aborting

“Activity ”指针似乎已损坏。

(我也希望env、activity、class和method都可以缓存)

我错过了什么?

最佳答案

这是一个线程问题。

SDL 在单独的线程中启动 SDLMain(),因此调用这些 Java 方法是有问题的!

理想的解决方案是以某种非线程模式运行 SDL,如此处所述 http://theorangeduck.com/page/issues-sdl-ios-and-android

但我已经通过在 SDLActivity 中公开 sendCommand 解决了我眼前的问题

public boolean sendCommand(int command, Object data)

并实现一个可以被处理的消息系统

public void progressMessage(String message) {
toast.setText(message);
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
}

public static boolean sendCommand(int command, String string) {
return SDLActivity.mSingleton.sendCommand(command, string);
}

@Override
protected boolean onUnhandledMessage(int command, Object param) {
boolean ok = true;
switch (command) {
case COMMAND_USER_PROGRESS:
progressMessage((String)param);
break;
default:
ok = false;
break;
}
return ok;
}

关于android - 使用 android 和 sdl 从 c++ 进行 java 回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27424633/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com