Trying to learn how to use the google maps api and I was following a tutorial on youtube. When I run the Given Code, then app closes & a Massage "... has stopped responding" Shows Up. Help Please.
Below is the code:
import com.google.android.gms.maps.*;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import android.app.Activity;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends FragmentActivity {
private GoogleMap mainMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpMap(); //line 27
}
private void setUpMap() {
if(mainMap == null) {
mainMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); //line 34
if(mainMap != null) {
startMap();
}
}
}
private void startMap() {
mainMap.setMyLocationEnabled(true);
mainMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria(); //retrieve provider
String provider = locationManager.getBestProvider(criteria, true); //name of best provider
Location myLocation = locationManager.getLastKnownLocation(provider);
double myLat = myLocation.getLatitude();
double myLong = myLocation.getLongitude();
LatLng latLng = new LatLng(myLat, myLong);
mainMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mainMap.animateCamera(CameraUpdateFactory.zoomTo(20));
mainMap.addMarker(new MarkerOptions().position(new LatLng(myLat, myLong)).title("You are here"));
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Here is the logcat:
01-18 12:16:20.915: E/AndroidRuntime(5163): FATAL EXCEPTION: main
01-18 12:16:20.915: E/AndroidRuntime(5163): Process: com.example.map, PID: 5163
01-18 12:16:20.915: E/AndroidRuntime(5163): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.map/com.example.map.MainActivity}: java.lang.NullPointerException
01-18 12:16:20.915: E/AndroidRuntime(5163): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
01-18 12:16:20.915: E/AndroidRuntime(5163): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2255)
01-18 12:16:20.915: E/AndroidRuntime(5163): at android.app.ActivityThread.access$800(ActivityThread.java:142)
01-18 12:16:20.915: E/AndroidRuntime(5163): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1203)
01-18 12:16:20.915: E/AndroidRuntime(5163): at android.os.Handler.dispatchMessage(Handler.java:102)
01-18 12:16:20.915: E/AndroidRuntime(5163): at android.os.Looper.loop(Looper.java:136)
01-18 12:16:20.915: E/AndroidRuntime(5163): at android.app.ActivityThread.main(ActivityThread.java:5118)
01-18 12:16:20.915: E/AndroidRuntime(5163): at java.lang.reflect.Method.invokeNative(Native Method)
01-18 12:16:20.915: E/AndroidRuntime(5163): at java.lang.reflect.Method.invoke(Method.java:515)
01-18 12:16:20.915: E/AndroidRuntime(5163): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
01-18 12:16:20.915: E/AndroidRuntime(5163): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
01-18 12:16:20.915: E/AndroidRuntime(5163): at dalvik.system.NativeStart.main(Native Method)
01-18 12:16:20.915: E/AndroidRuntime(5163): Caused by: java.lang.NullPointerException
01-18 12:16:20.915: E/AndroidRuntime(5163): at com.example.map.MainActivity.setUpMap(MainActivity.java:34)
01-18 12:16:20.915: E/AndroidRuntime(5163): at com.example.map.MainActivity.onCreate(MainActivity.java:27)
01-18 12:16:20.915: E/AndroidRuntime(5163): at android.app.Activity.performCreate(Activity.java:5275)
01-18 12:16:20.915: E/AndroidRuntime(5163): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-18 12:16:20.915: E/AndroidRuntime(5163): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
01-18 12:16:20.915: E/AndroidRuntime(5163): ... 11 more
here is the layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp" />
</LinearLayout>