0

我正在使用 cocos2d-x 在 android 中开发游戏。这是我的要求。

  1. VideoView 将覆盖 30% 的屏幕。
  2. Cocos2dxGlsurfaceview 将覆盖 70% 的屏幕。

我已经创建了这个 xml。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center" >

    <VideoView
        android:id="@+id/videoPlayer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="7" />

    <FrameLayout
        android:id="@+id/frmGame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:background="@android:color/transparent" >
    </FrameLayout>

</LinearLayout>

这是 Cocos2dxActivity.java,我在其中编辑 init() 方法以添加 VideoView。

package org.cocos2dx.lib;

import org.cocos2dx.lib.Cocos2dxHelper.Cocos2dxHelperListener;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.VideoView;

public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelperListener {
    // ===========================================================
    // Constants
    // ===========================================================

    private static final String TAG = Cocos2dxActivity.class.getSimpleName();

    // ===========================================================
    // Fields
    // ===========================================================

    private Cocos2dxGLSurfaceView mGLSurfaceView;
    private Cocos2dxHandler mHandler;
    private static Context sContext = null;
    private FrameLayout frmGame;
    private VideoView videoPlayer;

    public static Context getContext() {
        return sContext;
    }

    // ===========================================================
    // Constructors
    // ===========================================================

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sContext = this;
        this.mHandler = new Cocos2dxHandler(this);

        this.init();

        Cocos2dxHelper.init(this, this);
    }

    @Override
    public void runOnGLThread(final Runnable pRunnable) {
        this.mGLSurfaceView.queueEvent(pRunnable);
    }

    // ===========================================================
    // Methods
    // ===========================================================
    public void init() {

        View v = LayoutInflater.from(this).inflate(R.layout.activity_cocos2dx_video1, null);

        frmGame = (FrameLayout) v.findViewById(R.id.frmGame);
        videoPlayer = (VideoView) v.findViewById(R.id.videoPlayer);


        // 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
        this.mGLSurfaceView = this.onCreateView();

        // ...add to FrameLayout
        framelayout.addView(this.mGLSurfaceView);

        // Switch to supported OpenGL (ARGB888) mode on emulator
        if (isAndroidEmulator())
           this.mGLSurfaceView.setEGLConfigChooser(8 , 8, 8, 8, 16, 0);

        this.mGLSurfaceView.setCocos2dxRenderer(new Cocos2dxRenderer());
        this.mGLSurfaceView.setCocos2dxEditText(edittext);

        // Set framelayout as the content view
        frmGame.addView(framelayout,framelayout_params);
        setContentView(frmGame);
    }

    public Cocos2dxGLSurfaceView onCreateView() {
        return new Cocos2dxGLSurfaceView(this);
    }

   private final static boolean isAndroidEmulator() {
      String model = Build.MODEL;
      Log.d(TAG, "model=" + model);
      String product = Build.PRODUCT;
      Log.d(TAG, "product=" + product);
      boolean isEmulator = false;
      if (product != null) {
         isEmulator = product.equals("sdk") || product.contains("_sdk") || product.contains("sdk_");
      }
      Log.d(TAG, "isEmulator=" + isEmulator);
      return isEmulator;
   }

    // ===========================================================
    // Inner and Anonymous Classes
    // ===========================================================
}

但是当我尝试将 VideoView 与 Cocos2dxGlsurfaceview 应用程序在 init() 方法中崩溃时。

错误:孩子已经有父母。您必须先调用 removeview()。

请帮我解决我做错了什么。

4

1 回答 1

1
View v = LayoutInflater.from(this).inflate(R.layout.activity_cocos2dx_video1, null);

这段代码是关键点。

从这个文档中,我们可以知道返回View的是根视图,也就是说,这v是 的父视图frmGame

所以,为了解决你的问题,你可以更换

setContentView(frmGame);

setContentView(v);
于 2013-10-24T06:19:54.387 回答