Android Map version 2 issue-Collection of common programming errors
I am new in android.I create an application for MAP and i install vending.apk and gms.apk in emulator for running map in emulator. And also copy google-play-service_lib project in my eclipse and include google-play-service.zip in my project lib folder and include google-play-service.jar in project.In the time of running it shows error. Logcat is given below. Please help me to solve this problem.
public class MainActivity extends Activity{
GMapV2GetRouteDirection routedirection;
GoogleMap googlemap;
MarkerOptions markerOption;
LatLng PositionFrom;
LatLng PositionTo;
Document document;
MarkerOptions markerOptions;
Location location ;
/*static final LatLng Poprad = new LatLng(49.055, 20.299);
static final LatLng Prag = new LatLng(50.085, 14.458);*/
private GoogleMap map;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
routedirection=new GMapV2GetRouteDirection();
/*SupportMapFragment mapfragment=(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
googlemap=mapfragment.getMap();*/
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
googlemap.setMyLocationEnabled(true);
googlemap.getUiSettings().setZoomControlsEnabled(true);
googlemap.getUiSettings().setCompassEnabled(true);
googlemap.getUiSettings().setMyLocationButtonEnabled(true);
googlemap.getUiSettings().setAllGesturesEnabled(true);
googlemap.setTrafficEnabled(true);
googlemap.animateCamera(CameraUpdateFactory.zoomTo(10));
markerOption=new MarkerOptions();
PositionFrom=new LatLng(6777.7,8888.5);
PositionTo=new LatLng(4444.3, 3333.4);
GetRoutTask getRoute =new GetRoutTask();
getRoute.execute();
}
private class GetRoutTask extends AsyncTask{
private ProgressDialog pdialog;
String response = "";
protected void onPreExecute(){
pdialog=new ProgressDialog(MainActivity.this);
pdialog.setMessage("Loading.....");
pdialog.show();
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
document=routedirection.Getdocument(PositionFrom, PositionTo, GMapV2GetRouteDirection.MODE_DRIVING);
response = "Success";
return response;
}
protected void onPostExecute(String result) {
googlemap.clear();
if(response.equalsIgnoreCase("Success")){
ArrayList directionPoint = routedirection.getDirection(document);
PolylineOptions rectLine = new PolylineOptions().width(10).color(
Color.RED);
for (int i = 0; i < directionPoint.size(); i++) {
rectLine.add(directionPoint.get(i));
}
// Adding route on the map
googlemap.addPolyline(rectLine);
markerOptions.position(PositionFrom);
markerOptions.draggable(true);
googlemap.addMarker(markerOptions);
}
pdialog.dismiss();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Manifest
LOGCAT
07-26 14:24:22.261: W/dalvikvm(860): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
07-26 14:24:22.291: E/AndroidRuntime(860): FATAL EXCEPTION: main
07-26 14:24:22.291: E/AndroidRuntime(860): java.lang.RuntimeException: Unable to start activity ComponentInfo{example.map/example.map.MainActivity}: java.lang.NullPointerException
07-26 14:24:22.291: E/AndroidRuntime(860): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
07-26 14:24:22.291: E/AndroidRuntime(860): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
07-26 14:24:22.291: E/AndroidRuntime(860): at android.app.ActivityThread.access$600(ActivityThread.java:130)
07-26 14:24:22.291: E/AndroidRuntime(860): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
07-26 14:24:22.291: E/AndroidRuntime(860): at android.os.Handler.dispatchMessage(Handler.java:99)
07-26 14:24:22.291: E/AndroidRuntime(860): at android.os.Looper.loop(Looper.java:137)
07-26 14:24:22.291: E/AndroidRuntime(860): at android.app.ActivityThread.main(ActivityThread.java:4745)
07-26 14:24:22.291: E/AndroidRuntime(860): at java.lang.reflect.Method.invokeNative(Native Method)
07-26 14:24:22.291: E/AndroidRuntime(860): at java.lang.reflect.Method.invoke(Method.java:511)
07-26 14:24:22.291: E/AndroidRuntime(860): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-26 14:24:22.291: E/AndroidRuntime(860): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-26 14:24:22.291: E/AndroidRuntime(860): at dalvik.system.NativeStart.main(Native Method)
07-26 14:24:22.291: E/AndroidRuntime(860): Caused by: java.lang.NullPointerException
07-26 14:24:22.291: E/AndroidRuntime(860): at example.map.MainActivity.onCreate(MainActivity.java:49)
07-26 14:24:22.291: E/AndroidRuntime(860): at android.app.Activity.performCreate(Activity.java:5008)
07-26 14:24:22.291: E/AndroidRuntime(860): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
07-26 14:24:22.291: E/AndroidRuntime(860): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
07-26 14:24:22.291: E/AndroidRuntime(860): ... 11 more
07-26 14:25:04.591: I/Process(860): Sending signal. PID: 860 SIG: 9
-
You need to reference the google play services library project in your map project.
Copy the google-play services_lib library project to your workspace (folder where your android map project is). The library project can be found under the following path.
/extras/google/google_play_services/libproject/google-play-services_lib library project .
Import the library project to your eclipse
Click File > Import, select Android > Existing Android Code into Workspace, and browse the workspace import the library project. You can check if it is library project. Right click on the library project. Goto properties. Click Android on the left panel. You will see Is Library checked.
Right click on your android project. Goto properties. Choose Android on the left panel. Click on Add and browse the library project. Select the same. Click ok and apply
Edit:
Your min sdk is 8 So you should use
SupportMapFragment
.Your activity must extend
FragmentActivity
.SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); GoogleMap mMap = fm.getMap();
Also make sure you imported the below
import android.support.v4.app.FragmentActivity; import com.google.android.gms.maps.SupportMapFragment;
Originally posted 2013-11-16 20:49:32.