1

I have an app that has a custom list adapter. The list has 3 parts: an image, text, and a button.

  • When a row is clicked, I make an activity start,
  • and when the button is clicked, I want another activity to start, but it's a MapActivity.
  • Both activities must carry with them information passed via extras in the intent.
  • The first (clicking the row) activity works perfectly.
  • The second only works when starting a regular Activity.
  • When I try starting the MapActivity, the app crashes.
  • The code below is inside a custom adapter class, and the View received as parameter/argument in the OnClick method is a button (which is why I used the getParent).

If there's anything unclear, please let me know and I'll do my best to answer.


@Override
public void onClick(View view)
{
    View parentView = (View) view.getParent();
    switch (view.getId())
    {
        case R.id.boton_mapa_lugar:
            Intent intent = new Intent("android.intent.action.GOOGLEMAPS");

            String lat = ((TextView) parentView.findViewById(R.id.lat_lugar)).getText()
                    .toString();
            String lon = ((TextView) parentView.findViewById(R.id.lon_lugar)).getText()

            intent.putExtra(Lugares.DB_FIELD_LAT, lat);
            intent.putExtra(Lugares.DB_FIELD_LON, lon);

            parentView.getContext().startActivity(intent);
            break;
        default:
            //Code for starting activity when row is clicked...

    }
}

Here is the error log:

11-01 18:32:18.374: W/dalvikvm(14162): threadid=1: thread exiting with uncaught exception (group=0x4001d7d0)
11-01 18:32:18.393: E/AndroidRuntime(14162): FATAL EXCEPTION: main
11-01 18:32:18.393: E/AndroidRuntime(14162): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.turiston/com.example.turiston.GoogleMaps}: java.lang.NullPointerException
11-01 18:32:18.393: E/AndroidRuntime(14162):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-01 18:32:18.393: E/AndroidRuntime(14162):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-01 18:32:18.393: E/AndroidRuntime(14162):    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-01 18:32:18.393: E/AndroidRuntime(14162):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-01 18:32:18.393: E/AndroidRuntime(14162):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-01 18:32:18.393: E/AndroidRuntime(14162):    at android.os.Looper.loop(Looper.java:123)
11-01 18:32:18.393: E/AndroidRuntime(14162):    at android.app.ActivityThread.main(ActivityThread.java:4627)
11-01 18:32:18.393: E/AndroidRuntime(14162):    at java.lang.reflect.Method.invokeNative(Native Method)
11-01 18:32:18.393: E/AndroidRuntime(14162):    at java.lang.reflect.Method.invoke(Method.java:521)
11-01 18:32:18.393: E/AndroidRuntime(14162):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
11-01 18:32:18.393: E/AndroidRuntime(14162):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-01 18:32:18.393: E/AndroidRuntime(14162):    at dalvik.system.NativeStart.main(Native Method)
11-01 18:32:18.393: E/AndroidRuntime(14162): Caused by: java.lang.NullPointerException
11-01 18:32:18.393: E/AndroidRuntime(14162):    at org.apache.harmony.luni.util.FloatingPointParser.parseFloat(FloatingPointParser.java:301)
11-01 18:32:18.393: E/AndroidRuntime(14162):    at java.lang.Float.parseFloat(Float.java:291)
11-01 18:32:18.393: E/AndroidRuntime(14162):    at com.example.turiston.GoogleMaps.onCreate(GoogleMaps.java:37)
11-01 18:32:18.393: E/AndroidRuntime(14162):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-01 18:32:18.393: E/AndroidRuntime(14162):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-01 18:32:18.393: E/AndroidRuntime(14162):    ... 11 more

This is the OnCreate Method for GoogleMaps.java

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_google_maps);

    // Obtains the coordinates from the place from the previous Activity
    Bundle extras = getIntent().getExtras();
    float latitude; // = 19.2373f;
    float longitude; // = 68.82634f;

    // Variables to store the strings from the database
    String lat_coordinate;
    String lon_coordinate;

    // Assigns the values from the previous Activity to the two variables
    lat_coordinate = extras.getString("Latitude");
    lon_coordinate = extras.getString("Longitude");

    latitude = Float.parseFloat(lat_coordinate);
    longitude = Float.parseFloat(lon_coordinate);

    MapView mapView = (MapView) findViewById(R.id.mapview);

    // Enables zoom controls for the map
    mapView.setBuiltInZoomControls(true);

    List<Overlay> mapOverlays = mapView.getOverlays();
    Drawable drawable = this.getResources().getDrawable(
            R.drawable.androidmarker);

    MapsItemizedOverlay itemizedoverlay = new MapsItemizedOverlay(drawable,
            this);

    // Creates a GeoPoint to be used for the location of the place on the
    // map
    GeoPoint point = new GeoPoint((int) (latitude * 1E6),
            (int) (longitude * 1E6));

    // If you click on the Android guy it displays the following message
    OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!",
            "I'm in San Juan!");
    itemizedoverlay.addOverlay(overlayitem);
    mapOverlays.add(itemizedoverlay);
}
Shivam Kumar
  • 1,892
  • 2
  • 21
  • 33
Veris
  • 35
  • 1
  • 1
  • 6

1 Answers1

0

Do not use "android.intent.action.GOOGLEMAPS" as the action name. Create your own one with your package name in it so it will never collide with any other app ones.

Seems like the coordinates aren't reaching the GoogleMaps activity. Put Log.d() in the first activity just before starting the activity and in the second activity just before "latitude = Float.parseFloat(lat_coordinate);"

BTW, you are using constants for the extras names in the first activity and string literals in the second, very prone to error. Use always constants.

rgrocha
  • 1,461
  • 10
  • 19
  • Thanks. I finally fixed it. It was a simple, yet easily missable mistake. The strings "lat_coordinates" and "lon_coordinates" were both null, because the tags I was using for passing the extras were different. I was using "latitud" and "longitud" (constants DB_FIELD_LAT and DB_FIELD_LON), and my teammate, who was in charge of the other class (GoogleMaps.java) had used "latitude" and "longitude" instead (funny they're the same in different languages). – Veris Nov 01 '12 at 23:56
  • I just changed the tag names in my code to those GoogleMaps.java was receiving, like so: String LAT="Latitude"; String LON="Longitude"; And changed the putExtras to this: intent.putExtra(LAT, lat); intent.putExtra(LON, lon); Lastly, I wrote the intent like this: Intent intent = new Intent(parentView.getContext(), GoogleMaps.class); Thanks everyone! P.S. I can't answer my own question yet because of the reputation, but I wanted to let you know the problem was solved. – Veris Nov 01 '12 at 23:57
  • As I said, constants are your friends ;-) – rgrocha Nov 01 '12 at 23:58