我的代码过去可以正常工作,但突然停止工作,现在它给了我这个错误:
11-06 00:59:28.852: W/GooglePlayServicesUtil(9518): Google Play services out of date. Requires 4030500 but found 3266130
11-06 00:59:28.862: W/GooglePlayServicesUtil(9518): Google Play services out of date. Requires 4030500 but found 3266130
11-06 00:59:28.862: W/GooglePlayServicesUtil(9518): Google Play services out of date. Requires 4030500 but found 3266130
11-06 00:59:28.862: W/GooglePlayServicesUtil(9518): Google Play services out of date. Requires 4030500 but found 3266130
11-06 00:59:28.872: W/GooglePlayServicesUtil(9518): Google Play services out of date. Requires 4030500 but found 3266130
11-06 00:59:28.882: W/GooglePlayServicesUtil(9518): Google Play services out of date. Requires 4030500 but found 3266130
11-06 00:59:28.882: D/AndroidRuntime(9518): Shutting down VM
11-06 00:59:28.882: W/dalvikvm(9518): threadid=1: thread exiting with uncaught exception (group=0x41933898)
11-06 00:59:28.882: E/AndroidRuntime(9518): FATAL EXCEPTION: main
11-06 00:59:28.882: E/AndroidRuntime(9518): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.carfinder/com.example.carfinder.MainActivity}: java.lang.NullPointerException
11-06 00:59:28.882: E/AndroidRuntime(9518): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
11-06 00:59:28.882: E/AndroidRuntime(9518): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
11-06 00:59:28.882: E/AndroidRuntime(9518): at android.app.ActivityThread.access$700(ActivityThread.java:159)
11-06 00:59:28.882: E/AndroidRuntime(9518): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
11-06 00:59:28.882: E/AndroidRuntime(9518): at android.os.Handler.dispatchMessage(Handler.java:99)
11-06 00:59:28.882: E/AndroidRuntime(9518): at android.os.Looper.loop(Looper.java:137)
11-06 00:59:28.882: E/AndroidRuntime(9518): at android.app.ActivityThread.main(ActivityThread.java:5419)
11-06 00:59:28.882: E/AndroidRuntime(9518): at java.lang.reflect.Method.invokeNative(Native Method)
11-06 00:59:28.882: E/AndroidRuntime(9518): at java.lang.reflect.Method.invoke(Method.java:525)
11-06 00:59:28.882: E/AndroidRuntime(9518): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
11-06 00:59:28.882: E/AndroidRuntime(9518): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
11-06 00:59:28.882: E/AndroidRuntime(9518): at dalvik.system.NativeStart.main(Native Method)
11-06 00:59:28.882: E/AndroidRuntime(9518): Caused by: java.lang.NullPointerException
11-06 00:59:28.882: E/AndroidRuntime(9518): at com.example.carfinder.MainActivity.onCreate(MainActivity.java:38)
11-06 00:59:28.882: E/AndroidRuntime(9518): at android.app.Activity.performCreate(Activity.java:5372)
11-06 00:59:28.882: E/AndroidRuntime(9518): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
11-06 00:59:28.882: E/AndroidRuntime(9518): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
11-06 00:59:28.882: E/AndroidRuntime(9518): ... 11 more
这是我的 MainActivity.java
package com.example.carfinder;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import com.google.android.gms.location.LocationListener;
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;
public class MainActivity extends android.support.v4.app.FragmentActivity implements LocationListener {
GoogleMap googleMap;
LatLng myPosition ;
// add all necessary things
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// if Google Play Services are available then
// 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();
googleMap.getUiSettings().setZoomControlsEnabled(false);
// Enabling MyLocation Layer of Google Map
googleMap.setMyLocationEnabled(true);
// Getting LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Creating a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Getting the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
// Getting Current Location
Location location = locationManager.getLastKnownLocation(provider);
if(location!=null){
// Getting latitude of the current location
double latitude = location.getLatitude();
// Getting longitude of the current location
double longitude = location.getLongitude();
// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
myPosition = new LatLng(latitude, longitude);
googleMap.moveCamera(CameraUpdateFactory.newLatLng(myPosition));
// Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(16));
}
}
public void onClick_Clear(View v) {
// Removes all markers, overlays, and polylines from the map.
googleMap.clear();
}
public void onClick_Park(View v){
googleMap.addMarker(new MarkerOptions().position(myPosition).title("Parking Position"));
}
public void onClick_getDirections(View v){
}
public void onClick_Traffic(View v){
}
@Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
}
}
这是我的activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2012 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- This can go anywhere in your layout (see other demos for some examples). -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/message_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
class="com.google.android.gms.maps.SupportMapFragment" />
<Button
android:id="@+id/btnpark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:onClick="onClick_Park"
android:text="Park" />
<Button
android:id="@+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:onClick="onClick_Clear"
android:text="Clear" />
<Button
android:id="@+id/btnTraffic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:onClick="onClick_Traffic"
android:text="Traffic" />
<Button
android:id="@+id/btngetDirections"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="onClick_getDirections"
android:text="Get Directions" />
</RelativeLayout>
</LinearLayout>
最后是我的 CarFinder Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.carfinder"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- External storage for caching. -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!-- My Location -->
<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"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyCnnxM_6pT4ZCvRrvlQjN4gk74ccuTpslg"/>
<activity
android:name="com.example.carfinder.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>
</application>
</manifest>
该代码过去可以完美运行,但是当我更新 sdk 管理器中的工具时,我开始获取此代码。我试图删除该项目并再次执行此操作,但它仍然无法正常工作并给了我同样的错误。