使用LayoutGameActivity创建列表视图

时间:2013-04-22 08:20:44

标签: android android-listview andengine

我扩展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;

}

}

2 个答案:

答案 0 :(得分:1)

请参阅ANDEngine的菜单类

查看以下链接:

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

答案 1 :(得分: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;

    }