I am trying to use Android TMX Loader to load a game map. I keep having problems that are/seem to be because of the file not being loaded. I used the example provided, and just substituted my file, but no matter what path I use, it won’t load. I either get a file not found exception, or a null pointer exception on the line(s) that load the file. I’ve verified that the assets are being put into the APK with WinRar, and they are indeed there…
Here’s my code:
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();
}
}
Here is the latest LogCat stack:
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
What is it that’s going wrong? I can’t figure it out for the life of me.
Thank you so much for helping me!
here
you are trying to find
ImageViewMapImagebefore adding layout to current Activity so change your code as: