这是我的 onCreate() 方法。这两条注释的行之前用于启动地图,效果很好。后来我发现 Google 不再使用 LocationClient,而是使用 GoogleAPIClient。所以我改变了它。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button send = (Button) findViewById(R.id.button2);
if(initMap())
{
//mLocationClient = new LocationClient(this,this,this);
//mLocationClient.connect();
mGoogleApiClient =new GoogleApiClient.Builder(MainActivity.this)
.addApi(LocationServices.API)
.addConnectionCallbacks(MainActivity.this)
.addOnConnectionFailedListener(MainActivity.this)
.build();
}
else
{
Toast.makeText(this,"Map Not Available!",Toast.LENGTH_SHORT).show();
}
}
这是 initMap 方法
private boolean initMap(){
if(mMap==null)
{
//making Reference to Map Object
SupportMapFragment mapFrag = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
mMap=mapFrag.getMap();
}
return(mMap!=null);
}
这是 logcat 报告。
12-20 15:45:47.195: E/AndroidRuntime(1457): FATAL EXCEPTION: main
12-20 15:45:47.195: E/AndroidRuntime(1457): Process: com.example.mapsproject, PID: 1457
12-20 15:45:47.195: E/AndroidRuntime(1457): java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/gms/R$styleable;
12-20 15:45:47.195: E/AndroidRuntime(1457): at com.google.android.gms.maps.GoogleMapOptions.createFromAttributes(Unknown Source)
12-20 15:45:47.195: E/AndroidRuntime(1457): at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
12-20 15:45:47.195: E/AndroidRuntime(1457): at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2158)
12-20 15:45:47.195: E/AndroidRuntime(1457): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:297)
12-20 15:45:47.195: E/AndroidRuntime(1457): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
12-20 15:45:47.195: E/AndroidRuntime(1457): at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
12-20 15:45:47.195: E/AndroidRuntime(1457): at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
12-20 15:45:47.195: E/AndroidRuntime(1457): at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
12-20 15:45:47.195: E/AndroidRuntime(1457): at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
12-20 15:45:47.195: E/AndroidRuntime(1457): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)
12-20 15:45:47.195: E/AndroidRuntime(1457): at android.app.Activity.setContentView(Activity.java:2144)
12-20 15:45:47.195: E/AndroidRuntime(1457): at com.example.mapsproject.MainActivity.onCreate(MainActivity.java:71)
12-20 15:45:47.195: E/AndroidRuntime(1457): at android.app.Activity.performCreate(Activity.java:5933)
12-20 15:45:47.195: E/AndroidRuntime(1457): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
12-20 15:45:47.195: E/AndroidRuntime(1457): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
12-20 15:45:47.195: E/AndroidRuntime(1457): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
12-20 15:45:47.195: E/AndroidRuntime(1457): at android.app.ActivityThread.access$800(ActivityThread.java:144)
12-20 15:45:47.195: E/AndroidRuntime(1457): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
12-20 15:45:47.195: E/AndroidRuntime(1457): at android.os.Handler.dispatchMessage(Handler.java:102)
12-20 15:45:47.195: E/AndroidRuntime(1457): at android.os.Looper.loop(Looper.java:135)
12-20 15:45:47.195: E/AndroidRuntime(1457): at android.app.ActivityThread.main(ActivityThread.java:5221)
12-20 15:45:47.195: E/AndroidRuntime(1457): at java.lang.reflect.Method.invoke(Native Method)
12-20 15:45:47.195: E/AndroidRuntime(1457): at java.lang.reflect.Method.invoke(Method.java:372)
12-20 15:45:47.195: E/AndroidRuntime(1457): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
12-20 15:45:47.195: E/AndroidRuntime(1457): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
12-20 15:45:47.195: E/AndroidRuntime(1457): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.R$styleable" on path: DexPathList[[zip file "/data/app/com.example.mapsproject-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
12-20 15:45:47.195: E/AndroidRuntime(1457): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
12-20 15:45:47.195: E/AndroidRuntime(1457): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
12-20 15:45:47.195: E/AndroidRuntime(1457): at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
12-20 15:45:47.195: E/AndroidRuntime(1457): ... 25 more
12-20 15:45:47.195: E/AndroidRuntime(1457): Suppressed: java.lang.ClassNotFoundException: com.google.android.gms.R$styleable
12-20 15:45:47.195: E/AndroidRuntime(1457): at java.lang.Class.classForName(Native Method)
12-20 15:45:47.195: E/AndroidRuntime(1457): at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
12-20 15:45:47.195: E/AndroidRuntime(1457): at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
12-20 15:45:47.195: E/AndroidRuntime(1457): at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
12-20 15:45:47.195: E/AndroidRuntime(1457): ... 26 more
12-20 15:45:47.195: E/AndroidRuntime(1457): Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
12-20 15:45:51.501: I/Process(1457): Sending signal. PID: 1457 SIG: 9