2013-04-22 64 views
0

我擴展了LayoutGameActivity以創建一個列表視圖到一個引擎。但遇到錯誤使用LayoutGameActivity創建列表視圖

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; 

} 

}

回答

1

你需要使用renderSurfaceView在你的xml

添加到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; 

    } 
+0

VI,在activity_main.xml中? – 2013-04-22 08:54:49

+0

yes您正在使用的XML與您的活動 – 2013-04-22 09:00:58

+0

04-22 14:58:26.683:E/AndroidRuntime(11494):致命異常:主 04-22 14:58:26.683:E/AndroidRuntime(11494):java。 lang.RuntimeException:無法啓動活動ComponentInfo {com.example.jumper.game/com.ex.listview.ListViewTestActivity}:android.view.InflateException:二進制XML文件行#6:錯誤膨脹類org.anddev.andengine.opengl .view.RenderSurfaceView **得到這個錯誤現在 – 2013-04-22 09:02:31