现在我想在Java环境中的主OpenGL视图中添加一些Java视图。但是这对我来说并不容易。我认为我需要基本了解Java环境中视图、活动、意图等的工作原理。
更具体地说,我需要在运行时向我的OpenGL视图添加一个TextView(Java)。我尝试了以下方法,但当我调用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);
}
}
如果我尝试运行另一个活动或在public void testSetText()
函数中调用setContentView(R.layout.test_screen)
,我的游戏也会崩溃。
有人能给我一些好的建议吗?
更新:
感谢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环境,并请求添加某种视图(例如
TextView )(请参见 testSetText())。此时,我的应用程序崩溃了 - 带有crashlog:
WARN / dalvikvm(8352):threadid = 11:线程退出,未捕获
异常(group = 0x40015560)08-16 15:19:52.214:
ERROR / AndroidRuntime(8352):致命异常:GLThread 10 08-16
15:19:52.214:ERROR / AndroidRuntime(8352):
现在我还使用Admob,在Java端集成了它。当Admob通过一些委托(例如onReceiveAd)调用我的主类(myTest见上文)时 - 我尝试调用 testSetText()并在此时添加我的 TextView - 一切正常!
所以我认为这与线程有关。但我不是这个领域的专家,所以我非常需要一些帮助。
有什么建议吗?