0

I am developing an android application on Google maps. I have successfully displayed the MapView in the applciation in emulator. But when i tried to insert a pinpoint marker on the map, the following problem regarding " Unable to start activity ComponentInfo" occured . Here is my code:

package com.example.maps;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class MainActivity extends MapActivity {

MapView map;
MapController controller;
Drawable d;
List<Overlay> overlayList;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    map = (MapView)findViewById(R.id.mapRef);
    map.setBuiltInZoomControls(true);

    controller = map.getController();
    GeoPoint point = new GeoPoint(23129491,72541321);
    controller.animateTo(point);
    controller.setZoom(19);

    d = getResources().getDrawable(R.drawable.taxi);
    OverlayItem overlayItem =  new OverlayItem(point, "Hello there!","This is the taxi");
    drawPinpoint draw = new drawPinpoint(d, MainActivity.this);
    draw.insertPinpoint(overlayItem);
    overlayList.add(draw);

}

static class drawPinpoint extends ItemizedOverlay<OverlayItem>{

    private ArrayList<OverlayItem> pinpoints = new ArrayList<OverlayItem>();
    private Context c;

    public drawPinpoint(Drawable arg0) {
        super(boundCenter(arg0));
        // TODO Auto-generated constructor stub
    }

    public drawPinpoint(Drawable m, Context context) {
        // TODO Auto-generated constructor stub
        this(m);
        c = context;
    }

    @Override
    protected OverlayItem createItem(int i) {
        // TODO Auto-generated method stub
        return pinpoints.get(i);
    }

    @Override
    public int size() {
        // TODO Auto-generated method stub
        return pinpoints.size();
    }

    public void insertPinpoint(OverlayItem item){
        pinpoints.add(item);
        this.populate();
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

}

This is my manifest file :

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.maps"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
<uses-library android:name="com.google.android.maps"/>
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

And this is my stack trace for NullPounterException:

08-26 14:19:11.760: W/dalvikvm(528): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 08-26 14:19:11.790: E/AndroidRuntime(528): FATAL EXCEPTION: main 08-26 14:19:11.790: E/AndroidRuntime(528): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.maps/com.example.maps.MainActivity}: java.lang.NullPointerException 08-26 14:19:11.790: E/AndroidRuntime(528): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 08-26 14:19:11.790: E/AndroidRuntime(528): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 08-26 14:19:11.790: E/AndroidRuntime(528): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 08-26 14:19:11.790: E/AndroidRuntime(528): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 08-26 14:19:11.790: E/AndroidRuntime(528): at android.os.Handler.dispatchMessage(Handler.java:99) 08-26 14:19:11.790: E/AndroidRuntime(528): at android.os.Looper.loop(Looper.java:123) 08-26 14:19:11.790: E/AndroidRuntime(528): at android.app.ActivityThread.main(ActivityThread.java:4627) 08-26 14:19:11.790: E/AndroidRuntime(528): at java.lang.reflect.Method.invokeNative(Native Method) 08-26 14:19:11.790: E/AndroidRuntime(528): at java.lang.reflect.Method.invoke(Method.java:521) 08-26 14:19:11.790: E/AndroidRuntime(528): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 08-26 14:19:11.790: E/AndroidRuntime(528): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 08-26 14:19:11.790: E/AndroidRuntime(528): at dalvik.system.NativeStart.main(Native Method) 08-26 14:19:11.790: E/AndroidRuntime(528): Caused by: java.lang.NullPointerException 08-26 14:19:11.790: E/AndroidRuntime(528): at com.example.maps.MainActivity.onCreate(MainActivity.java:42) 08-26 14:19:11.790: E/AndroidRuntime(528): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 08-26 14:19:11.790: E/AndroidRuntime(528): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 08-26 14:19:11.790: E/AndroidRuntime(528): ... 11 more

1
  • Your stacktrace is identifying a Null at line 42 of your code in the MainActivity.onCreate() method. Put a breakpoint before this and see what variable is null. Aug 26, 2012 at 10:01

1 Answer 1

0

I think line 42 of your code is:

overlayList.add(draw);

and you have not created an overlayList object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.