我正在尝试在我的 android 应用程序中添加地图。我遵循了所有必要的先决条件。
所以我做了一个简单的活动来测试它。
public class SpaineMapActivity extends Activity {
static final LatLng HAMBURG = new LatLng(53.558, 9.927);
private GoogleMap map;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
}
}
并使用 xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
</RelativeLayout>
它工作正常。没问题。但是当我尝试将它集成到我的其他应用程序时,当然会加载所有库。但即使只是在 setContentView() 部分,我已经得到了错误。两种布局都是一样的。
public class act_map extends FragmentActivity {
static final LatLng HAMBURG = new LatLng(53.558, 9.927);
private GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lyt_map);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
}
}
我收到此错误
02-21 19:19:25.650: E/AndroidRuntime(23097): java.lang.NoClassDefFoundError: com.google.android.gms.R$styleable
02-21 19:19:25.650: E/AndroidRuntime(23097): at com.google.android.gms.maps.GoogleMapOptions.createFromAttributes(Unknown Source)
02-21 19:19:25.650: E/AndroidRuntime(23097): at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
02-21 19:19:25.650: E/AndroidRuntime(23097): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:269)
02-21 19:19:25.650: E/AndroidRuntime(23097): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
02-21 19:19:25.650: E/AndroidRuntime(23097): at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
02-21 19:19:25.650: E/AndroidRuntime(23097): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
02-21 19:19:25.650: E/AndroidRuntime(23097): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
02-21 19:19:25.650: E/AndroidRuntime(23097): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:257)
02-21 19:19:25.650: E/AndroidRuntime(23097): at android.app.Activity.setContentView(Activity.java:1867)
02-21 19:19:25.650: E/AndroidRuntime(23097): at dev.foogle.act_map.onCreate(act_map.java:21)
02-21 19:19:25.650: E/AndroidRuntime(23097): at android.app.Activity.performCreate(Activity.java:5008)
02-21 19:19:25.650: E/AndroidRuntime(23097): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
02-21 19:19:25.650: E/AndroidRuntime(23097): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
02-21 19:19:25.650: E/AndroidRuntime(23097): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2215)
02-21 19:19:25.650: E/AndroidRuntime(23097): at android.app.ActivityThread.access$600(ActivityThread.java:145)
02-21 19:19:25.650: E/AndroidRuntime(23097): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1211)
02-21 19:19:25.650: E/AndroidRuntime(23097): at android.os.Handler.dispatchMessage(Handler.java:99)
02-21 19:19:25.650: E/AndroidRuntime(23097): at android.os.Looper.loop(Looper.java:137)
02-21 19:19:25.650: E/AndroidRuntime(23097): at android.app.ActivityThread.main(ActivityThread.java:4978)
02-21 19:19:25.650: E/AndroidRuntime(23097): at java.lang.reflect.Method.invokeNative(Native Method)
02-21 19:19:25.650: E/AndroidRuntime(23097): at java.lang.reflect.Method.invoke(Method.java:511)
02-21 19:19:25.650: E/AndroidRuntime(23097): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
02-21 19:19:25.650: E/AndroidRuntime(23097): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
02-21 19:19:25.650: E/AndroidRuntime(23097): at dalvik.system.NativeStart.main(Native Method)