我使用Eclipse、Android SDK,我有一个类不把每个代码都放在主活动中:
blablabla
import blablabla
..
..
public class Mhelper {
static final LatLng HAMBURG = new LatLng(53.558, 9.927);
static final LatLng KIEL = new LatLng(53.551, 9.993);
private GoogleMap map;
Context mContext;
public Mhelper(Context ctx) {
this.mContext= ctx;
}
public void pinpointlocation(Context context){
map = ((SupportMapFragment) ((FragmentActivity) mContext).getSupportFragmentManager().findFragmentById(R.id.mapview))
.getMap();
Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
.title("Hamburg"));
Marker kiel = map.addMarker(new MarkerOptions()
.position(KIEL)
.title("Kiel")
.snippet("Kiel is cool")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_launcher)));
// Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
// Zoom in, animating the camera.
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"
...
...
...
...
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="KEY"
/>
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/button2" />
</RelativeLayout>
程序编译和安装没有任何错误,但当我在模拟器中运行应用程序时,我得到:
blablabla
04-23 11:36:32.408: E/AndroidRuntime(2515): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hach/com.example.hach.MainActivity}: android.view.InflateException: Binary XML file line #48: Error inflating class com.google.android.maps.MapView
blablabla
04-23 11:36:32.408: E/AndroidRuntime(2515): Caused by: android.view.InflateException: Binary XML file line #48: Error inflating class com.google.android.maps.MapView
blablabla
04-23 11:36:32.408: E/AndroidRuntime(2515): Caused by: java.lang.IllegalArgumentException: MapViews can only be created inside instances of MapActivity.
我在堆栈中搜索,找到这个,这个和这个。
第一个只是承认他不应该使用viewmap,第二个被告知阅读这个参考,这是一个很好的参考,但我阅读了它,找不到任何可以应用于我的项目的解决方案。第三个说谷歌地图安卓API v2是一个好的参考。
我发现了这个链接,这是一个很好的例子,但我不想使用制表符,应该吗?除了线路
import com.google.android.maps.MapActivity;
是当我创建新类并且想要扩展MapActivity时的问题,IDE不知道MapActivity。
在不完全更改应用程序的情况下,我能对我当前的项目做些什么吗?在我编码时显示地图并指出位置?我应该扩展MapActivity并将其添加到项目中吗?有没有像mapview这样简单的解决方案?
我想好了。我首先删除了地图视图(CommonsWare是对的,它在V1中),然后我添加了
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
显示。然后它就起作用了。这个问题是由一些不合格的网站引起的,这些网站试图在没有测试的情况下发布教程。