0

我扩展了 LayoutGameActivity 以在 andengine 中创建一个列表视图。但是出现错误

E/AndroidRuntime(8719): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.example.jumper.game/com.ex.listview.ListViewTestActivity}: java.lang.NullPointerException
E/AndroidRuntime(8719): 在 android .app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)

我的 activity_main.xml 看起来:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@+id/id_list_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</RelativeLayout>

我的活动看起来像:

public class ListViewTestActivity extends LayoutGameActivity {

public Camera camera;
private ResourcesManager resourcesManager;
public final static int CAMERA_WIDTH = 1280;
public final static int CAMERA_HEIGHT = 720;
public ListView aListView;
@Override
public EngineOptions onCreateEngineOptions() {

    camera = new Camera(0, 0, 1280, 720);
    EngineOptions engineOptions = new EngineOptions(true,
            ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(),
            this.camera);
    engineOptions.getAudioOptions().setNeedsMusic(true).setNeedsSound(true);
    engineOptions.setWakeLockOptions(WakeLockOptions.SCREEN_ON);
    return engineOptions;

}
@Override
public void onCreateResources(
        OnCreateResourcesCallback pOnCreateResourcesCallback)
        throws Exception {
}
@Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
        throws Exception {

    this.mEngine.registerUpdateHandler(new FPSLogger());
    final Scene scene = new Scene();

    aListView = (ListView) findViewById(R.id.list_view);
    String[] items = new String[] { "Item 1", "Item 2", "Item 3" };
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, items);
    aListView.setAdapter(adapter);

    pOnCreateSceneCallback.onCreateSceneFinished(scene);
}

@Override
public void onPopulateScene(Scene pScene,
        OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {

    pOnPopulateSceneCallback.onPopulateSceneFinished();
}

@Override
protected int getLayoutID() {

    // my xml layout
    return R.layout.activity_main;
}

@Override
protected int getRenderSurfaceViewID() {

    return 0;

}

}

4

2 回答 2

1

参见 ANDEngine 的菜单类

查看以下链接:

http://apachejava.blogspot.com/2012/03/andengine-menu-scrolling-example.html

于 2013-04-22T08:35:36.717 回答
1

您需要在 xml 中使用 renderSurfaceView

添加到 XML

<org.anddev.andengine.opengl.view.RenderSurfaceView 
        android:id="@+id/xmllayoutRenderSurfaceView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerInParent="true"
        android:gravity="bottom" 

        />

并在你的活动课上得到它

@Override
    protected int getRenderSurfaceViewID() {
return R.id.xmllayoutRenderSurfaceView;

    } 
于 2013-04-22T08:44:20.580 回答