Reputation: 1
Im having trouble getting my map to display on an Android App. I have watched videos and read many forums (including this one) on troubleshooting the issues. My code isnt displaying any errors but every time I run it whether it be on an phone, tablet or emulator it says "Unfortunately, stuff has stopped". Any help or assistance would be greatly appreciated. SN: SDK and Google Play Services are up to date.
package com.stuff;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
// TODO Auto-generated method stub
if (googleMap == null) {
googleMap = ((SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
if (googleMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
// TODO Auto-generated method stub
googleMap.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager)
getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location myLocation = locationManager.getLastKnownLocation(provider);
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
double latitude = myLocation.getLatitude();
double longitude = myLocation.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(10));
googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude,
longitude)).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;
}
}
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.stuff"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="17" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- The following two permissions are required for location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyAQz_W-qNqgpXp8SZG3sqAt_YuVVSalCg8"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
07-25 01:54:10.266: E/Trace(1074): error opening trace file: No such file or directory (2)
07-25 01:54:11.085: W/GooglePlayServicesUtil(1074): Google Play services is missing.
07-25 01:54:11.085: W/GooglePlayServicesUtil(1074): Google Play services is missing.
07-25 01:54:11.105: W/GooglePlayServicesUtil(1074): Google Play services is missing.
07-25 01:54:11.105: W/GooglePlayServicesUtil(1074): Google Play services is missing.
07-25 01:54:11.117: W/GooglePlayServicesUtil(1074): Google Play services is missing.
07-25 01:54:11.117: W/GooglePlayServicesUtil(1074): Google Play services is missing.
07-25 01:54:11.145: W/GooglePlayServicesUtil(1074): Google Play services is missing.
07-25 01:54:11.165: W/GooglePlayServicesUtil(1074): Google Play services is missing.
07-25 01:54:11.208: W/GooglePlayServicesUtil(1074): Google Play services is missing.
07-25 01:54:11.325: D/AndroidRuntime(1074): Shutting down VM
07-25 01:54:11.325: W/dalvikvm(1074): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
07-25 01:54:11.355: E/AndroidRuntime(1074): FATAL EXCEPTION: main
07-25 01:54:11.355: E/AndroidRuntime(1074): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.stuff/com.stuff.MainActivity}: java.lang.NullPointerException
07-25 01:54:11.355: E/AndroidRuntime(1074): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-25 01:54:11.355: E/AndroidRuntime(1074): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-25 01:54:11.355: E/AndroidRuntime(1074): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-25 01:54:11.355: E/AndroidRuntime(1074): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-25 01:54:11.355: E/AndroidRuntime(1074): at android.os.Handler.dispatchMessage(Handler.java:99)
07-25 01:54:11.355: E/AndroidRuntime(1074): at android.os.Looper.loop(Looper.java:137)
07-25 01:54:11.355: E/AndroidRuntime(1074): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-25 01:54:11.355: E/AndroidRuntime(1074): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 01:54:11.355: E/AndroidRuntime(1074): at java.lang.reflect.Method.invoke(Method.java:511)
07-25 01:54:11.355: E/AndroidRuntime(1074): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-25 01:54:11.355: E/AndroidRuntime(1074): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-25 01:54:11.355: E/AndroidRuntime(1074): at dalvik.system.NativeStart.main(Native Method)
07-25 01:54:11.355: E/AndroidRuntime(1074): Caused by: java.lang.NullPointerException
07-25 01:54:11.355: E/AndroidRuntime(1074): at com.stuff.MainActivity.setUpMapIfNeeded(MainActivity.java:33)
07-25 01:54:11.355: E/AndroidRuntime(1074): at com.stuff.MainActivity.onCreate(MainActivity.java:26)
07-25 01:54:11.355: E/AndroidRuntime(1074): at android.app.Activity.performCreate(Activity.java:5104)
07-25 01:54:11.355: E/AndroidRuntime(1074): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-25 01:54:11.355: E/AndroidRuntime(1074): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-25 01:54:11.355: E/AndroidRuntime(1074): ... 11 more
07-25 01:54:11.536: D/dalvikvm(1074): GC_CONCURRENT freed 140K, 9% free 2701K/2960K, paused 19ms+4ms, total 216ms
07-25 01:54:13.585: I/Process(1074): Sending signal. PID: 1074 SIG: 9
Upvotes: 0
Views: 2493
Reputation: 230
try to change you layout to this:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
or
if (googleMap == null) {
googleMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();
}
Upvotes: 1
Reputation: 161
try this code.
SupportMapFragment mFragment;
mFragment = new SupportMapFragment() {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if ((map = mFragment.getMap()) != null) {
setUpMap();
}
}
};
Upvotes: 0
Reputation: 133580
Your getMap()
returns null.
public final GoogleMap getMap ()
Gets the underlying GoogleMap that is tied to the view wrapped by this fragment.
Returns the GoogleMap. Null if the view of the fragment is not yet ready. This can happen if the fragment lifecyle have not gone through onCreateView(LayoutInflater, ViewGroup, Bundle) yet. This can also happen if Google Play services is not available. If Google Play services becomes available afterwards and the fragment have gone through onCreateView(LayoutInflater, ViewGroup, Bundle), calling this method again will initialize and return the GoogleMap.
So you need to wait till fragment is ready and also check for availability google play services before initialing GooleMap object.
Upvotes: 1