当我在模拟器上运行此应用程序时,它显示缺少谷歌播放服务。但是当我在真实设备上运行相同的应用程序时,它意外停止应用程序使其崩溃这里是我的地图的代码
`
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application
android:allowBackup="true"
android:icon="@drawable/loc_finder"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-library android:name="com.google.android.maps" />
<service android:name="jade.android.MicroRuntimeService" />
<activity
android:name="com.example.location_finder.Login_Activity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="in.wptrafficanalyzer.proximitymapv2.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>
<activity
android:name=".ProximityActivity"
android:label="@string/app_name" >
<intent-filter >
<action android:name="in.wptrafficanalyzer.activity.proximity"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".NotificationView"
android:label="@string/app_name" >
<intent-filter >
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="com.example.location_finder.New_User_Activity" >
</activity>
<activity android:name="com.example.location_finder.Welcome_Activity" >
</activity>
<activity android:name="com.example.location_finder.Search_Friend_Activity" >
</activity>
<activity android:name="com.example.location_finder.Search_Location_Activity" >
</activity>
<activity android:name="com.example.location_finder.GPSLocatorActivity" >
</activity>
<activity android:name="chat.client.gui.MainActivity" />
<activity android:name="chat.client.gui.ChatActivity" />
<activity android:name="chat.client.gui.ChatClientActivity" />
<activity android:name="chat.client.gui.ParticipantsActivity" />
</application>
<uses-permission
android:name="in.wptrafficanalyzer.proximitymapv2.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="in.wptrafficanalyzer.proximitymapv2.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.VIBRATE" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyDgah79JaIABl54K8V8HfB7UzopKVwtgzw"/>
</manifest>
`
MainActivity.java
public class MainActivity extends FragmentActivity
{
GoogleMap googleMap;
LocationManager locationManager;
PendingIntent pendingIntent;
SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Getting Google Play availability status
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
// Showing status
if(status!=ConnectionResult.SUCCESS){ // Google Play Services are not available
int requestCode = 10;
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
dialog.show();
}else { // Google Play Services are available
// Getting reference to the SupportMapFragment of activity_main.xml
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
// Getting GoogleMap object from the fragment
googleMap = fm.getMap();
// Enabling MyLocation Layer of Google Map
googleMap.setMyLocationEnabled(true);
// Getting LocationManager object from System Service LOCATION_SERVICE
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Opening the sharedPreferences object
sharedPreferences = getSharedPreferences("location", 0);
// Getting stored latitude if exists else return 0
String lat = sharedPreferences.getString("lat", "0");
// Getting stored longitude if exists else return 0
String lng = sharedPreferences.getString("lng", "0");
// Getting stored zoom level if exists else return 0
String zoom = sharedPreferences.getString("zoom", "0");
// If coordinates are stored earlier
if(!lat.equals("0")){
// Drawing circle on the map
drawCircle(new LatLng(Double.parseDouble(lat), Double.parseDouble(lng)));
// Drawing marker on the map
drawMarker(new LatLng(Double.parseDouble(lat), Double.parseDouble(lng)));
// Moving CameraPosition to previously clicked position
googleMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(Double.parseDouble(lat), Double.parseDouble(lng))));
// Setting the zoom level in the map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(Float.parseFloat(zoom)));
}
googleMap.setOnMapClickListener(new OnMapClickListener() {
//@Override
public void onMapClick(LatLng point) {
// Removes the existing marker from the Google Map
googleMap.clear();
// Drawing marker on the map
drawMarker(point);
// Drawing circle on the map
drawCircle(point);
// This intent will call the activity ProximityActivity
Intent proximityIntent = new Intent("in.wptrafficanalyzer.activity.proximity");
// Creating a pending intent which will be invoked by LocationManager when the specified region is
// entered or exited
pendingIntent = PendingIntent.getActivity(getBaseContext(), 0, proximityIntent,Intent.FLAG_ACTIVITY_NEW_TASK);
// Setting proximity alert
// The pending intent will be invoked when the device enters or exits the region 20 meters
// away from the marked point
// The -1 indicates that, the monitor will not be expired
locationManager.addProximityAlert(point.latitude, point.longitude, 20, -1, pendingIntent);
/** Opening the editor object to write data to sharedPreferences */
SharedPreferences.Editor editor = sharedPreferences.edit();
/** Storing the latitude of the current location to the shared preferences */
editor.putString("lat", Double.toString(point.latitude));
/** Storing the longitude of the current location to the shared preferences */
editor.putString("lng", Double.toString(point.longitude));
/** Storing the zoom level to the shared preferences */
editor.putString("zoom", Float.toString(googleMap.getCameraPosition().zoom));
/** Saving the values stored in the shared preferences */
editor.commit();
Toast.makeText(getBaseContext(), "Proximity Alert is added", Toast.LENGTH_SHORT).show();
}
});
googleMap.setOnMapLongClickListener(new OnMapLongClickListener() {
//@Override
public void onMapLongClick(LatLng point) {
Intent proximityIntent = new Intent("in.wptrafficanalyzer.activity.proximity");
pendingIntent = PendingIntent.getActivity(getBaseContext(), 0, proximityIntent,Intent.FLAG_ACTIVITY_NEW_TASK);
// Removing the proximity alert
locationManager.removeProximityAlert(pendingIntent);
// Removing the marker and circle from the Google Map
googleMap.clear();
// Opening the editor object to delete data from sharedPreferences
SharedPreferences.Editor editor = sharedPreferences.edit();
// Clearing the editor
editor.clear();
// Committing the changes
editor.commit();
Toast.makeText(getBaseContext(), "Proximity Alert is removed", Toast.LENGTH_LONG).show();
}
});
}
}
private void drawMarker(LatLng point){
// Creating an instance of MarkerOptions
MarkerOptions markerOptions = new MarkerOptions();
// Setting latitude and longitude for the marker
markerOptions.position(point);
// Adding marker on the Google Map
googleMap.addMarker(markerOptions);
}
private void drawCircle(LatLng point){
// Instantiating CircleOptions to draw a circle around the marker
CircleOptions circleOptions = new CircleOptions();
// Specifying the center of the circle
circleOptions.center(point);
// Radius of the circle
circleOptions.radius(20);
// Border color of the circle
circleOptions.strokeColor(Color.BLACK);
// Fill color of the circle
circleOptions.fillColor(0x30ff0000);
// Border width of the circle
circleOptions.strokeWidth(2);
// Adding the circle to the GoogleMap
googleMap.addCircle(circleOptions);
}
@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;
}
}
日志猫
05-04 22:36:22.820: D/SensorManager(29794): registerListener :: handle = 0 name= LSM330DLC 3-axis Accelerometer delay= 200000 Listener= android.view.OrientationEventListener$SensorEventListenerImpl@42c2ce28
05-04 22:36:22.870: D/dalvikvm(29794): GC_CONCURRENT freed 209K, 9% free 13173K/14407K, paused 5ms+1ms, total 17ms
05-04 22:36:22.875: D/AndroidRuntime(29794): Shutting down VM
05-04 22:36:22.875: W/dalvikvm(29794): threadid=1: thread exiting with uncaught exception (group=0x41e542a0)
05-04 22:36:22.885: E/AndroidRuntime(29794): FATAL EXCEPTION: main
05-04 22:36:22.885: E/AndroidRuntime(29794): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.location_finder/in.wptrafficanalyzer.proximitymapv2.MainActivity}: java.lang.NullPointerException
05-04 22:36:22.885: E/AndroidRuntime(29794): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
05-04 22:36:22.885: E/AndroidRuntime(29794): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
05-04 22:36:22.885: E/AndroidRuntime(29794): at android.app.ActivityThread.access$600(ActivityThread.java:140)
05-04 22:36:22.885: E/AndroidRuntime(29794): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
05-04 22:36:22.885: E/AndroidRuntime(29794): at android.os.Handler.dispatchMessage(Handler.java:99)
05-04 22:36:22.885: E/AndroidRuntime(29794): at android.os.Looper.loop(Looper.java:137)
05-04 22:36:22.885: E/AndroidRuntime(29794): at android.app.ActivityThread.main(ActivityThread.java:4898)
05-04 22:36:22.885: E/AndroidRuntime(29794): at java.lang.reflect.Method.invokeNative(Native Method)
05-04 22:36:22.885: E/AndroidRuntime(29794): at java.lang.reflect.Method.invoke(Method.java:511)
05-04 22:36:22.885: E/AndroidRuntime(29794): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
05-04 22:36:22.885: E/AndroidRuntime(29794): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
05-04 22:36:22.885: E/AndroidRuntime(29794): at dalvik.system.NativeStart.main(Native Method)
05-04 22:36:22.885: E/AndroidRuntime(29794): Caused by: java.lang.NullPointerException
05-04 22:36:22.885: E/AndroidRuntime(29794): at in.wptrafficanalyzer.proximitymapv2.MainActivity.onCreate(MainActivity.java:55)
05-04 22:36:22.885: E/AndroidRuntime(29794): at android.app.Activity.performCreate(Activity.java:5206)
05-04 22:36:22.885: E/AndroidRuntime(29794): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
05-04 22:36:22.885: E/AndroidRuntime(29794): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
05-04 22:36:22.885: E/AndroidRuntime(29794): ... 11 more