我有和发动机表面视图及其重复编码活动。现在我想在简单的活动中调用这个andengine View,我不想通过意图调用和引擎活动。就像Custome View
一样**XML View:**
<?xml version="1.0" encoding="UTF-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="@+id/btn_get_involved"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/tv_xmllayoutexample_above"
android:textSize="20sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#FFF"
android:orientation="horizontal"
android:padding="2px" >
<org.andengine.opengl.view.RenderSurfaceView
android:id="@+id/xmllayoutexample_rendersurfaceview"
android:layout_width="240px"
android:layout_height="240px"
android:layout_margin="10px" />
</LinearLayout>
<TextView
android:id="@+id/btn_get_involved"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/tv_xmllayoutexample_below"
android:textSize="20sp" />
</LinearLayout>
</ScrollView>
**AndEngine Activity:**
public class XMLLayoutExample extends SimpleLayoutGameActivity {
private static final int CAMERA_WIDTH = 720;
private static final int CAMERA_HEIGHT = 480;
private BitmapTextureAtlas mBitmapTextureAtlas;
private ITextureRegion mParticleTextureRegion;
@Override
protected int getLayoutID() {
return R.layout.xmllayoutexample;
}
@Override
protected void onSetContentView() {
}
@Override
protected int getRenderSurfaceViewID() {
return R.id.xmllayoutexample_rendersurfaceview;
}
@Override
public EngineOptions onCreateEngineOptions() {
Toast.makeText(this, "Touch the screen to move the particlesystem.", Toast.LENGTH_LONG).show();
final Camera camera = new Camera(0, 0, XMLLayoutExample.CAMERA_WIDTH, XMLLayoutExample.CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(XMLLayoutExample.CAMERA_WIDTH, XMLLayoutExample.CAMERA_HEIGHT), camera);
}
@Override
protected void onCreateResources() {
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 32, 32, TextureOptions.BILINEAR);
this.mParticleTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "particle_point.png", 0, 0);
this.mBitmapTextureAtlas.load();
}
@Override
protected Scene onCreateScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
final Scene scene = new Scene();
final CircleOutlineParticleEmitter particleEmitter = new CircleOutlineParticleEmitter(XMLLayoutExample.CAMERA_WIDTH * 0.5f, (XMLLayoutExample.CAMERA_HEIGHT * 0.5f) + 20, 80);
final SpriteParticleSystem particleSystem = new SpriteParticleSystem(particleEmitter, 60, 60, 360, this.mParticleTextureRegion, this.getVertexBufferObjectManager());
scene.setOnSceneTouchListener(new IOnSceneTouchListener() {
@Override
public boolean onSceneTouchEvent(final Scene pScene, final TouchEvent pSceneTouchEvent) {
particleEmitter.setCenter(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
return true;
}
});
particleSystem.addParticleInitializer(new ColorParticleInitializer<Sprite>(1, 0, 0));
particleSystem.addParticleInitializer(new AlphaParticleInitializer<Sprite>(0));
particleSystem.addParticleInitializer(new BlendFunctionParticleInitializer<Sprite>(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE));
particleSystem.addParticleInitializer(new VelocityParticleInitializer<Sprite>(-2, 2, -20, -10));
particleSystem.addParticleInitializer(new RotationParticleInitializer<Sprite>(0.0f, 360.0f));
particleSystem.addParticleInitializer(new ExpireParticleInitializer<Sprite>(6));
particleSystem.addParticleModifier(new ScaleParticleModifier<Sprite>(0, 5, 1.0f, 2.0f));
particleSystem.addParticleModifier(new ColorParticleModifier<Sprite>(0, 3, 1, 1, 0, 0.5f, 0, 0));
particleSystem.addParticleModifier(new ColorParticleModifier<Sprite>(4, 6, 1, 1, 0.5f, 1, 0, 1));
particleSystem.addParticleModifier(new AlphaParticleModifier<Sprite>(0, 1, 0, 1));
particleSystem.addParticleModifier(new AlphaParticleModifier<Sprite>(5, 6, 1, 0));
scene.attachChild(particleSystem);
return scene;
}
}
**MySimple Activity:(Main Launcher Activity)**
public class TestActivity extends Activity {
/** Called when the activity is first created. */
Button b;
ImageView iv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.xmlLayout);
}
}
LogCat查看:
我正在尝试这个但却给出错误:
12-19 11:05:47.876: E/AndroidRuntime(31074): FATAL EXCEPTION: main
12-19 11:05:47.876: E/AndroidRuntime(31074): java.lang.NullPointerException
12-19 11:05:47.876: E/AndroidRuntime(31074): at org.andengine.opengl.view.RenderSurfaceView.onMeasure(RenderSurfaceView.java:68)
12-19 11:05:47.876: E/AndroidRuntime(31074): at android.view.View.measure(View.java:15524)
12-19 11:05:47.876: E/AndroidRuntime(31074): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5109)
12-19 11:05:47.876: E/AndroidRuntime(31074): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
12-19 11:05:47.876: E/AndroidRuntime(31074): at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1038)
12-19 11:05:47.876: E/AndroidRuntime(31074): at android.widget.LinearLayout.onMeasure(LinearLayout.java:576)
12-19 11:05:47.876: E/AndroidRuntime(31074): at android.view.View.measure(View.java:15524)
12-19 11:05:47.876: E/AndroidRuntime(31074): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5109)
12-19 11:05:47.876: E/AndroidRuntime(31074): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
12-19 11:05:47.876: E/AndroidRuntime(31074): at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
12-19 11:05:47.876: E/AndroidRuntime(31074): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
12-19 11:05:47.876: E/AndroidRuntime(31074): at android.view.View.measure(View.java:15524)
12-19 11:05:47.876: E/AndroidRuntime(31074): at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1477)
12-19 11:05:47.876: E/AndroidRuntime(31074): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-19 11:05:47.876: E/AndroidRuntime(31074): at android.widget.ScrollView.onMeasure(ScrollView.java:393)
12-19 11:05:47.876: E/AndroidRuntime(31074): at android.view.View.measure(View.java:15524)
12-19 11:05:47.876: E/AndroidRuntime(31074): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5109)
12-19 11:05:47.876: E/AndroidRuntime(31074): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-19 11:05:47.876: E/AndroidRuntime(31074): at android.view.View.measure(View.java:15524)
12-19 11:05:47.876: E/AndroidRuntime(31074): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5109)
12-19 11:05:47.876: E/AndroidRuntime(31074): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
12-19 11:05:47.876: E/AndroidRuntime(31074): at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
12-19 11:05:47.876: E/AndroidRuntime(31074): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
12-19 11:05:47.876: E/AndroidRuntime(31074): at android.view.View.measure(View.java:15524)
12-19 11:05:47.876: E/AndroidRuntime(31074): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5109)
12-19 11:05:47.876: E/AndroidRuntime(31074): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-19 11:05:47.876: E/AndroidRuntime(31074): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2397)
12-19 11:05:47.876: E/AndroidRuntime(31074): at android.view.View.measure(View.java:15524)
12-19 11:05:47.876: E/AndroidRuntime(31074): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1986)
12-19 11:05:47.876: E/AndroidRuntime(31074): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1227)
12-19 11:05:47.876: E/AndroidRuntime(31074): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1400)
12-19 11:05:47.876: E/AndroidRuntime(31074): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1120)
12-19 11:05:47.876: E/AndroidRuntime(31074): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4604)
12-19 11:05:47.876: E/AndroidRuntime(31074): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
12-19 11:05:47.876: E/AndroidRuntime(31074): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
12-19 11:05:47.876: E/AndroidRuntime(31074): at android.view.Choreographer.doFrame(Choreographer.java:525)
12-19 11:05:47.876: E/AndroidRuntime(31074): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
12-19 11:05:47.876: E/AndroidRuntime(31074): at android.os.Handler.handleCallback(Handler.java:615)
12-19 11:05:47.876: E/AndroidRuntime(31074): at android.os.Handler.dispatchMessage(Handler.java:92)
12-19 11:05:47.876: E/AndroidRuntime(31074): at android.os.Looper.loop(Looper.java:137)
12-19 11:05:47.876: E/AndroidRuntime(31074): at android.app.ActivityThread.main(ActivityThread.java:4921)
12-19 11:05:47.876: E/AndroidRuntime(31074): at java.lang.reflect.Method.invokeNative(Native Method)
12-19 11:05:47.876: E/AndroidRuntime(31074): at java.lang.reflect.Method.invoke(Method.java:511)
12-19 11:05:47.876: E/AndroidRuntime(31074): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
12-19 11:05:47.876: E/AndroidRuntime(31074): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
12-19 11:05:47.876: E/AndroidRuntime(31074): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
不要扩展SimpleBaseGameActivity
。使用LayoutGameActivity
类进行扩展。
实施以下方法:
@Override
protected int getLayoutID()
{
return R.layout.my_layout;
}
@Override
protected int getRenderSurfaceViewID()
{
return R.id.xml_rendersurfaceview;
}
my_layout内容:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<org.anddev.andengine.opengl.view.RenderSurfaceView
android:id="@+id/xml_rendersurfaceview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="50dip"
android:layout_alignParentTop="true">
</RelativeLayout>
</RelativeLayout>
并在此布局中添加布局内容。