-1

我正在尝试使用 Android TMX Loader 加载游戏地图。我一直遇到/似乎是因为文件未加载的问题。我使用了提供的示例,只是替换了我的文件,但无论我使用什么路径,它都不会加载。我要么得到一个文件未找到异常,要么在加载文件的行上出现一个空指针异常。我已经验证资产正在使用 WinRar 放入 APK,它们确实在那里......

这是我的代码:

ImageView mapView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    loadWorld("World.tmx");

    setContentView(R.layout.activity_main);
}

/*
 * public void displayMap() { Display display =
 * getWindowManager().getDefaultDisplay(); Point size = new Point();
 * display.getSize(size); int width = size.x; int height = size.y;
 * 
 * }
 */

public void loadWorld(String path) {
    // Start the parser, get back TMX data object
    TileMapData t = TMXLoader.readTMX(path, this);

    mapView = (ImageView) findViewById(R.id.MapImage);

    // Create a Bitmap from the tilemap data
    Bitmap mapImage = TMXLoader.createBitmap(t, this, 0, t.layers.size());

    // Set the imageview to show the map, if we have one
    if (mapImage != null) {
        mapView.setImageBitmap(mapImage);
    }
    // Map loading problem, inform the user.
    else {
        Toast errorMessage = Toast.makeText(getApplicationContext(),
                "Map could not be loaded", Toast.LENGTH_LONG);
        errorMessage.show();
    }
}

这是最新的 LogCat 堆栈:

12-31 07:44:25.046: E/AndroidRuntime(18074): FATAL EXCEPTION: main
12-31 07:44:25.046: E/AndroidRuntime(18074): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fionaheiss.shovelshovel/com.fionaheiss.shovelshovel.DisplayMap}: java.lang.NullPointerException
12-31 07:44:25.046: E/AndroidRuntime(18074):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2006)
12-31 07:44:25.046: E/AndroidRuntime(18074):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2031)
12-31 07:44:25.046: E/AndroidRuntime(18074):    at android.app.ActivityThread.access$600(ActivityThread.java:126)
12-31 07:44:25.046: E/AndroidRuntime(18074):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1166)
12-31 07:44:25.046: E/AndroidRuntime(18074):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-31 07:44:25.046: E/AndroidRuntime(18074):    at android.os.Looper.loop(Looper.java:137)
12-31 07:44:25.046: E/AndroidRuntime(18074):    at android.app.ActivityThread.main(ActivityThread.java:4486)
12-31 07:44:25.046: E/AndroidRuntime(18074):    at java.lang.reflect.Method.invokeNative(Native Method)
12-31 07:44:25.046: E/AndroidRuntime(18074):    at java.lang.reflect.Method.invoke(Method.java:511)
12-31 07:44:25.046: E/AndroidRuntime(18074):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-31 07:44:25.046: E/AndroidRuntime(18074):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-31 07:44:25.046: E/AndroidRuntime(18074):    at dalvik.system.NativeStart.main(Native Method)
12-31 07:44:25.046: E/AndroidRuntime(18074): Caused by: java.lang.NullPointerException
12-31 07:44:25.046: E/AndroidRuntime(18074):    at com.fionaheiss.shovelshovel.DisplayMap.loadWorld(DisplayMap.java:38)
12-31 07:44:25.046: E/AndroidRuntime(18074):    at com.fionaheiss.shovelshovel.DisplayMap.onCreate(DisplayMap.java:20)
12-31 07:44:25.046: E/AndroidRuntime(18074):    at android.app.Activity.performCreate(Activity.java:4635)
12-31 07:44:25.046: E/AndroidRuntime(18074):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-31 07:44:25.046: E/AndroidRuntime(18074):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
12-31 07:44:25.046: E/AndroidRuntime(18074):    ... 11 more

出了什么问题?我这辈子都想不通。

非常感谢你帮助我!

4

2 回答 2

1

这里

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    loadWorld("World.tmx");  //<< here

    setContentView(R.layout.activity_main);
}

ImageView MapImage 您在将布局添加到当前活动之前尝试查找,因此将代码更改为:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    setContentView(R.layout.activity_main);

    loadWorld("file:///android_asset/World.tmx");
}
于 2012-12-31T15:37:49.217 回答
0

这意味着 t 或 t.layers 为空。

于 2012-12-31T15:37:46.277 回答