我已经使用 ndk 和 cocos2dx 将我的 iphone 应用程序移植到了 android。这就像一个魅力,我认为 cocos2dx 非常酷!
现在我想在 Java 环境中向我的主 opengl 视图中添加一些 Java 视图。这对我来说并不真正有效。我认为我需要有关在 Java 环境中具有视图、活动、意图等工作的基本知识。
具体来说,我需要TextView
在运行时将 (java) 添加到我的 opengl 视图中。我尝试了以下方法,但是当我调用函数 void 时它崩溃了testSetText()
。
public class myTest extends Cocos2dxActivity{
private static final String TAG = "MY_TEST";
private FrameLayout mainFrame;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Log.e(TAG, "onCreate");
String packageName = getApplication().getPackageName();
super.setPackageName(packageName);
mainFrame = new FrameLayout(this);
mGLView = new Cocos2dxGLSurfaceView(this);
mainFrame.addView(mGLView);
RelativeLayout base = new RelativeLayout(this);
base.addView(mainFrame);
setContentView(base);
}
private GLSurfaceView mGLView;
static {
System.loadLibrary("cocos2d");
System.loadLibrary("cocosdenshion");
System.loadLibrary("game");
}
@Override
protected void onPause() {
super.onPause();
Log.i("TAG"," onPause");
mGLView.onPause();
}
@Override
protected void onResume() {
super.onResume();
Log.i("TAG"," onResume");
mGLView.onResume();
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.e(TAG, "onStart");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.e(TAG, "onStop");
}
public void testSetText(){
Log.e(TAG, "testSetText");
TextView textView = new TextView(this);
textView.setText("Hello, Android");
LinearLayout testLayout = new LinearLayout(this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
testLayout.addView(textView ,lp);
mainFrame.addView(testLayout);
}
}
如果我改为尝试运行另一个活动或调用我setContentView(R.layout.test_screen)
的function public void testSetText()
游戏也会崩溃。
有人可以在这里给我一些好的建议吗?
更新:
感谢 Macarse,这里是堆栈跟踪:
08-16 15:19:52.121: INFO/TAG(8352): canITalktoPIT
08-16 15:19:52.121: ERROR/MY_APP(8352): test call PIT 2
08-16 15:19:52.203: WARN/dalvikvm(8352): threadid=11: thread exiting with uncaught exception (group=0x40015560)
08-16 15:19:52.214: ERROR/AndroidRuntime(8352): FATAL EXCEPTION: GLThread 10
08-16 15:19:52.214: ERROR/AndroidRuntime(8352): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
08-16 15:19:52.214: ERROR/AndroidRuntime(8352): at android.view.ViewRoot.checkThread(ViewRoot.java:2932)
08-16 15:19:52.214: ERROR/AndroidRuntime(8352): at android.view.ViewRoot.requestLayout(ViewRoot.java:629)
08-16 15:19:52.214: ERROR/AndroidRuntime(8352): at android.view.View.requestLayout(View.java:8267)
08-16 15:19:52.214: ERROR/AndroidRuntime(8352): at android.view.View.requestLayout(View.java:8267)
08-16 15:19:52.214: ERROR/AndroidRuntime(8352): at android.view.View.requestLayout(View.java:8267)
08-16 15:19:52.214: ERROR/AndroidRuntime(8352): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:257)
08-16 15:19:52.214: ERROR/AndroidRuntime(8352): at android.view.View.requestLayout(View.java:8267)
08-16 15:19:52.214: ERROR/AndroidRuntime(8352): at android.view.ViewGroup.addView(ViewGroup.java:1869)
08-16 15:19:52.214: ERROR/AndroidRuntime(8352): at android.view.ViewGroup.addView(ViewGroup.java:1851)
08-16 15:19:52.214: ERROR/AndroidRuntime(8352): at dk.comp.testApp.testAppB.calling(testAppB.java:289)
08-16 15:19:52.214: ERROR/AndroidRuntime(8352): at dk.comp.testApp.SigletonJohn.canITalktoPIT(SigletonJohn.java:43)
08-16 15:19:52.214: ERROR/AndroidRuntime(8352): at org.cocos2dx.lib.testAppJavaCppComunication.pitTestJNI(testAppJavaCppComunication.java:47)
08-16 15:19:52.214: ERROR/AndroidRuntime(8352): at org.cocos2dx.lib.Cocos2dxActivity.pitTestJNI(Cocos2dxActivity.java:177)
08-16 15:19:52.214: ERROR/AndroidRuntime(8352): at org.cocos2dx.lib.Cocos2dxRenderer.nativeTouchesEnd(Native Method)
08-16 15:19:52.214: ERROR/AndroidRuntime(8352): at org.cocos2dx.lib.Cocos2dxRenderer.handleActionUp(Cocos2dxRenderer.java:49)
08-16 15:19:52.214: ERROR/AndroidRuntime(8352): at org.cocos2dx.lib.Cocos2dxGLSurfaceView$9.run(Cocos2dxGLSurfaceView.java:288)
08-16 15:19:52.214: ERROR/AndroidRuntime(8352): at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1326)
08-16 15:19:52.214: ERROR/AndroidRuntime(8352): at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1118)
08-16 15:19:52.238: WARN/ActivityManager(116): Force finishing activity dk.comp.testApp/.testAppB
08-16 15:19:52.242: INFO/SOUND(8352): PAUSE
08-16 15:19:52.246: INFO/TAG(8352): onPause
更新#2:
好的,我仍在处理这个问题,只是无法修复它:(但我发现了一些有趣的东西:
所以总结一下我的问题:我的 android 应用程序有一个运行完美的 cocos2dx 场景。然后当用户按下按钮时:
myBut = CCMenuItemImage::itemFromNormalImage("some.png", "some.png.png", this, menu_selector(Cocos2dMenuScene::butPushed));
我通过 JNI 调用 Java 环境,并请求添加某种视图(例如 a TextView
)(参见 参考资料testSetText()
)。此时我的应用程序崩溃了 - 带有崩溃日志:
WARN/dalvikvm(8352): threadid=11: thread exiting with unaught exception (group=0x40015560) 08-16 15:19:52.214: ERROR/AndroidRuntime(8352): FATAL EXCEPTION: GLThread 10 08-16 15:19:52.214 :错误/AndroidRuntime(8352):
现在我也使用集成在 Java 端的 Admob。当 Admob 通过某个委托(例如 onReceiveAd )调用我的主类( myTest 见上文)时 - 我已经尝试调用testSetText()
并添加我TextView
的 - 一切正常!
所以我认为这与线程有关 - 但我不是这方面的专家,所以我非常需要一些帮助。
有什么建议么?