moagrius / TileView

TileView is a subclass of android.view.ViewGroup that asynchronously displays, pans and zooms tile-based images. Plugins are available for features like markers, hotspots, and path drawing.
MIT License
1.46k stars 337 forks source link

Crash when setCacheEnabled(..) is set to true #14

Closed braj008 closed 11 years ago

braj008 commented 11 years ago

When I load app with setCacheEnabled(..) to true, its crashing at below method

  private void addBitmapToMemoryCache( String key, Bitmap bitmap ) {        
    if ( getBitmapFromMemoryCache( key ) == null ) {
    // crashing here
        memoryCache.put( key, bitmap );
    }
}

Here, both key and bitmap are null. May be some null check should work.

klattimer commented 11 years ago

If you're seeing this you're probably getting out of memory errors first, if your app is memory intensive then you may want to try adding largeHeap to the android manifest. I had this problem caused by loading up an apk extension file which required the zip decompressor in memory, thus leading to an oom error and then to this.

I think a simple null check here would help too.

braj008 commented 11 years ago

I am sure it's not about OOM error. Logcat message clearly shows its a NullPointerException as below

08-23 13:08:22.172: E/AndroidRuntime(22880): FATAL EXCEPTION: AsyncTask 08-23 13:08:22.172: E/AndroidRuntime(22880): java.lang.RuntimeException: An error occured while executing doInBackground() 08-23 13:08:22.172: E/AndroidRuntime(22880): at com.qozix.os.AsyncTask$3.done(AsyncTask.java:276) 08-23 13:08:22.172: E/AndroidRuntime(22880): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273) 08-23 13:08:22.172: E/AndroidRuntime(22880): at java.util.concurrent.FutureTask.setException(FutureTask.java:124) 08-23 13:08:22.172: E/AndroidRuntime(22880): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307) 08-23 13:08:22.172: E/AndroidRuntime(22880): at java.util.concurrent.FutureTask.run(FutureTask.java:137) 08-23 13:08:22.172: E/AndroidRuntime(22880): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 08-23 13:08:22.172: E/AndroidRuntime(22880): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 08-23 13:08:22.172: E/AndroidRuntime(22880): at java.lang.Thread.run(Thread.java:856) 08-23 13:08:22.172: E/AndroidRuntime(22880): Caused by: java.lang.NullPointerException: key == null || value == null 08-23 13:08:22.172: E/AndroidRuntime(22880): at android.support.v4.util.LruCache.put(LruCache.java:117) 08-23 13:08:22.172: E/AndroidRuntime(22880): at com.qozix.tileview.tiles.TileCache.addBitmapToMemoryCache(TileCache.java:117) 08-23 13:08:22.172: E/AndroidRuntime(22880): at com.qozix.tileview.tiles.TileCache.addBitmap(TileCache.java:72) 08-23 13:08:22.172: E/AndroidRuntime(22880): at com.qozix.tileview.tiles.Tile.decode(Tile.java:75) 08-23 13:08:22.172: E/AndroidRuntime(22880): at com.qozix.tileview.tiles.TileManager.decodeIndividualTile(TileManager.java:308) 08-23 13:08:22.172: E/AndroidRuntime(22880): at com.qozix.tileview.tiles.TileRenderTask.doInBackground(TileRenderTask.java:51) 08-23 13:08:22.172: E/AndroidRuntime(22880): at com.qozix.tileview.tiles.TileRenderTask.doInBackground(TileRenderTask.java:1) 08-23 13:08:22.172: E/AndroidRuntime(22880): at com.qozix.os.AsyncTask$2.call(AsyncTask.java:262) 08-23 13:08:22.172: E/AndroidRuntime(22880): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 08-23 13:08:22.172: E/AndroidRuntime(22880): ... 4 more

moagrius commented 11 years ago

Thanks for the note - I'll take a look as soon as I have some time - I probably just missed something when migrating. If you find a fix before then please post.

yolapop commented 11 years ago

anyone found the fix?

Thx b4

moagrius commented 11 years ago

I just tried and am unable to reproduce - it works fine for me. I suspect there's probably a bad file request (perhaps the result over overly-aggressive intersection detection, which was slightly modified from the previous version).

If either of you @yolapop or @braj008 want to post a problem app somewhere, I'd be interested in taking a look / debugging - otherwise I'd suggest trying the null checks mentioned earlier:

private void addBitmapToMemoryCache( String key, Bitmap bitmap ) {        
    if ( key == null || bitmap == null ) {
        return;
    }
    if ( getBitmapFromMemoryCache( key ) == null ) {
    // crashing here
        memoryCache.put( key, bitmap );
    }

If that works, post back and I'll update the repo.

Thanks

yolapop commented 11 years ago

Worked for me :dancers:

moagrius commented 11 years ago

cool - I've committed the change as well. will leave issue open for a while in case anyone wants to post an app for debug

braj008 commented 11 years ago

Above mentioned solution is enough to solve the problem. I have been using following solution.

private void addBitmapToMemoryCache( String key, Bitmap bitmap ) {

    if ( getBitmapFromMemoryCache( key ) == null && bitmap != null ) {
        memoryCache.put( key, bitmap );
    }
} 
Tristus1er commented 11 years ago

I would better suggest this fix:

In: DetailLevel in getIntersections function:
Replace

        int startingRow = (int) Math.floor( viewport.top / offsetHeight );
        int endingRow = (int) Math.ceil( viewport.bottom / offsetHeight );
        int startingColumn = (int) Math.floor( viewport.left / offsetWidth );
        int endingColumn = (int) Math.ceil( viewport.right / offsetWidth );

By:

        int startingRow = (int) Math.floor( viewport.top / offsetHeight );
        int endingRow = (int) Math.ceil( viewport.bottom / offsetHeight ) - 1;
        int startingColumn = (int) Math.floor( viewport.left / offsetWidth );
        int endingColumn = (int) Math.ceil( viewport.right / offsetWidth ) - 1;

As tailes are from 0 to n

moagrius commented 11 years ago

awesome, big thanks - i'll try this out soon and if it works consistently i'll post a new commit

braj008 commented 11 years ago

@Tristus1er solution worked. Shall I close the issue?

moagrius commented 11 years ago

@braj008 i'll leave it open as a reminder until i test and commit the patch. thanks

moagrius commented 11 years ago

updated in the latest release