Thursday, October 31, 2013

java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.

Name: java.lang.IllegalStateException
Reason: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
Stack Trace:
0 java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
1 at android.database.CursorWindow.nativeGetString(Native Method)
2 at android.database.CursorWindow.getString(CursorWindow.java:438)
3 at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
4 at android.database.CursorWrapper.getString(CursorWrapper.java:114)
5 at com.travelzoo.android.ui.MapDealsActivity$1.onLoadFinished(MapXXXXActivity.java:****)
6 at com.travelzoo.android.ui.MapDealsActivity$1.onLoadFinished(MapXXXXActivity.java:1)
7 at android.support.v4.app.LoaderManagerImpl$LoaderInfo.callOnLoadFinished(LoaderManager.java:427)
8 at android.support.v4.app.LoaderManagerImpl$LoaderInfo.onLoadComplete(LoaderManager.java:395)
9 at android.support.v4.content.Loader.deliverResult(Loader.java:104)
10 at android.support.v4.content.CursorLoader.deliverResult(CursorLoader.java:73)
11 at android.support.v4.content.CursorLoader.deliverResult(CursorLoader.java:35)
12 at android.support.v4.content.AsyncTaskLoader.dispatchOnLoadComplete(AsyncTaskLoader.java:223)
13 at android.support.v4.content.AsyncTaskLoader$LoadTask.onPostExecute(AsyncTaskLoader.java:61)
14 at android.support.v4.content.ModernAsyncTask.finish(ModernAsyncTask.java:461)
15 at android.support.v4.content.ModernAsyncTask.access$500(ModernAsyncTask.java:47)
16 at android.support.v4.content.ModernAsyncTask$InternalHandler.handleMessage(ModernAsyncTask.java:474)
17 at android.os.Handler.dispatchMessage(Handler.java:99)
18 at android.os.Looper.loop(Looper.java:137)
19 at android.app.ActivityThread.main(ActivityThread.java:5328)
20 at java.lang.reflect.Method.invokeNative(Native Method)
21 at java.lang.reflect.Method.invoke(Method.java:511)
22 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
23 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
24 at dalvik.system.NativeStart.main(Native Method)



Fix

0. Try to position cursor by moveToFirst before reading data from it.

1. Close the cursor after if ( c.moveToFirst()) {}

2. check for null.e,g; if (c != null && c.moveToFirst()) {}

3. check for count.e,g; (c != null && c.getCount() >0 && c.moveToFirst()){}

Tuesday, October 15, 2013

Google Map V2 Android Tips and Ticks for beginners

1) How to set zoom controls?
googleMap.setOnCameraChangeListener(new OnCameraChangeListener() {
           
     public void onCameraChange(CameraPosition arg0) {
    googleMap.animateCamera(CameraUpdateFactory.zoomTo(8));
        googleMap.setOnCameraChangeListener(MySampleMapActivity.this);
      }
});
2) How to add marker at certain point?
mapView.addMarker(new MarkerOptions().position(new LatLng(lat, lng))
.title(dealTitle)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_menu_directions))
.snippet(dealPrice)
);
3) How to display my location icon?
googleMap.setMyLocationEnabled(true);
4) How to set map type?
a. googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
b. googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
 
c. googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
d. googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
5) How to animate to certain position?
final LatLng pos = new LatLng(LAT, LNG);
googleMap.setOnCameraChangeListener(new OnCameraChangeListener() {
        public void onCameraChange(CameraPosition arg0) {
            googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(pos, 11));
        }
    });
6) How to add multiple points?

final LatLngBounds.Builder builder = new LatLngBounds.Builder();
//Number of latitude and longitude in for loop
for(;;){
final LatLng pos = new LatLng(lat,lng);
 builder.include(pos);
googleMap.addMarker(new MarkerOptions()
                        .position(pos)
                        .title("TITLE")
                        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
}

Then,
googleMap.setOnCameraChangeListener(new OnCameraChangeListener() {
               
                public void onCameraChange(CameraPosition arg0) {
                    googleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 20);
                    googleMap.setOnCameraChangeListener(null);
                }
            });
7) HOw to add info window?
static final LatLng MY_LOCATION = new LatLng(LAT, LNG);
Marker pune = mMap.addMarker(new MarkerOptions()
                          .position(Pune)
                          .title("Pune")
                          .snippet("YOUR TEXT HERE")
                         .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));

Source:  http://mobisys.in/blog/2013/04/google-maps-android-api-v2-different-operations/

Google V2 Maps Zoom Control

The code will look like:
googleMap.setOnCameraChangeListener(new OnCameraChangeListener() {



public void onCameraChange(CameraPosition arg0) {
googleMap.animateCamera(CameraUpdateFactory.zoomTo(8));
googleMap.setOnCameraChangeListener(MySampleMapActivity.this);
}
});

Explanation:


public final void setOnCameraChangeListener (GoogleMap.OnCameraChangeListener listener)

Sets a callback that's invoked when the camera changes.
Parameters
listener The callback that's invoked when the camera changes. To unset the callback, use null.
Also,
public static interface
GoogleMap.OnCameraChangeListener
com.google.android.gms.maps.GoogleMap.OnCameraChangeListener
Class Overview
Defines signatures for methods that are called when the camera changes position.

Summary
Public Methods
abstract void onCameraChange(CameraPosition position)
Called after the camera position has changed.
public abstract void onCameraChange (CameraPosition position)

Called after the camera position has changed. During an animation, this listener may not be notified of intermediate camera positions. It is always called for the final position in the animation.

This is called on the main thread.

Parameters
position The CameraPosition at the end of the last camera change.


And Finally,

public final void animateCamera (CameraUpdate update, int durationMs,GoogleMap.CancelableCallback callback)

Moves the map according to the update with an animation over a specified duration, and calls an optional callback on completion. See CameraUpdateFactory for a set of updates.

If getCameraPosition() is called during the animation, it will return the current location of the camera in flight.

Parameters
durationMs The duration of the animation in milliseconds. This must be strictly positive, otherwise an IllegalArgumentException will be thrown.
callback An optional callback to be notified from the main thread when the animation stops. If the animation stops due to its natural completion, the callback will be notified withonFinish(). If the animation stops due to interruption by a later camera movement or a user gesture, onCancel() will be called. The callback should not attempt to move or animate the camera in its cancellation method.
public final void animateCamera (CameraUpdate update,GoogleMap.CancelableCallback callback)

Animates the movement of the camera from the current position to the position defined in the update and calls an optional callback on completion. See CameraUpdateFactory for a set of updates.

During the animation, a call to getCameraPosition() returns an intermediate location of the camera.

Parameters
update The change that should be applied to the camera.
callback The callback to invoke from the main thread when the animation stops. If the animation completes normally, onFinish() is called; otherwise, onCancel() is called. Do not update or animate the camera from within onCancel().
public final void animateCamera (CameraUpdate update)

Animates the movement of the camera from the current position to the position defined in the update. During the animation, a call to getCameraPosition() returns an intermediate location of the camera.

See CameraUpdateFactory for a set of updates.

Parameters
update The change that should be applied to the camera.


Wednesday, October 2, 2013

ActionBarSherlock


ActionBarSherlock

ActionBarSherlock is an standalone library designed to facilitate the use of the action bar design pattern across all versions of Android through a single API.
The library will automatically use the native ActionBar implementation on Android 4.0 or later. For previous versions which do not include ActionBar, a custom action bar implementation based on the sources of Ice Cream Sandwich will automatically be wrapped around the layout. This allows you to easily develop an application with an action bar for every version of Android from 2.x and up.
See http://actionbarsherlock.com for more information.