即使打开一个简单的地图应用程序(无论是在AVD虚拟设备上还是在真正的Android手机上),我都遇到了麻烦。使用Eclipse / ADT最新安装。
代码或多或少直接来自GoogleMaps v2的书籍章节(Wei-Meng Lee / Wrox撰写的“Android应用程序开发手册”)。几周前,在SSD发生故障之前,我简要地运行了类似的代码。我把书中的代码保持不变 - 下载/导入等。其他简单的helloworld和其他应用程序在AVD等上运行良好。
我有:
似乎没有任何帮助。经过4周的挫折之后,我现在正在寻求帮助(如果这是一个简单的错误,可能会得到宽恕!)
我的清单文件显示:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.learn2develop.googlemaps"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="19" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<permission
android:name="net.learn2develop.googlemaps.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="net.learn2develop.googlemaps.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" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="net.learn2develop.googlemaps.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>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="my_API_Key_was_put_here"
/>
<!-- next meta data tag added after consulting stack overflow forums -->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
我的布局如下:
<LinearLayout 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" >
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</LinearLayout>
我的Mainactivity.java如下。
package net.learn2develop.googlemaps;
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.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;
@SuppressWarnings("unused")
public class MainActivity extends FragmentActivity {
GoogleMap map;
private static final LatLng GOLDEN_GATE_BRIDGE = new LatLng(37.828891,
-122.485884);
private static final LatLng APPLE = new LatLng(37.3325004578, -122.03099823);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// alt is ..map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
map = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
if (map == null) {
Toast.makeText(this, "Google Maps not available",
Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_sethybrid:
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
break;
case R.id.menu_showtraffic:
map.setTrafficEnabled(true);
break;
case R.id.menu_zoomin:
map.animateCamera(CameraUpdateFactory.zoomIn());
break;
case R.id.menu_zoomout:
map.animateCamera(CameraUpdateFactory.zoomOut());
break;
case R.id.menu_gotolocation:
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(GOLDEN_GATE_BRIDGE) // Sets the center of the map to
// Golden Gate Bridge
.zoom(17) // Sets the zoom
.bearing(90) // Sets the orientation of the camera to east
.tilt(30) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
map.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
break;
case R.id.menu_addmarker:
// ---using the default marker---
/*
* map.addMarker(new MarkerOptions() .position(GOLDEN_GATE_BRIDGE)
* .title("Golden Gate Bridge") .snippet("San Francisco")
* .icon(BitmapDescriptorFactory
* .defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
*/
map.addMarker(new MarkerOptions()
.position(GOLDEN_GATE_BRIDGE)
.title("Golden Gate Bridge")
.snippet("San Francisco")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_launcher)));
break;
case R.id.menu_getcurrentlocation:
// ---get your current location and display a blue dot---
map.setMyLocationEnabled(true);
break;
case R.id.menu_showcurrentlocation:
Location myLocation = map.getMyLocation();
LatLng myLatLng = new LatLng(myLocation.getLatitude(),
myLocation.getLongitude());
CameraPosition myPosition = new CameraPosition.Builder()
.target(myLatLng).zoom(17).bearing(90).tilt(30).build();
map.animateCamera(CameraUpdateFactory.newCameraPosition(myPosition));
break;
case R.id.menu_lineconnecttwopoints:
// ---add a marker at Apple---
map.addMarker(new MarkerOptions()
.position(APPLE)
.title("Apple")
.snippet("Cupertino")
.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
// ---draw a line connecting Apple and Golden Gate Bridge---
map.addPolyline(new PolylineOptions()
.add(GOLDEN_GATE_BRIDGE, APPLE).width(5).color(Color.RED));
break;
}
return true;
}
}
我真的认为我在这里寻求帮助之前已经尝试了一切。 (我曾经在20世纪80年代早期手工编写Z80汇编代码并将其手工转换为Hex - 与此相比简直就是这样........)
非常感谢你们的帮助!
稍后添加 - 这是我的错误日志
05-02 07:33:32.177: I/Google Maps Android API(1058): Google Play services client version: 4323000
05-02 07:33:32.217: I/Google Maps Android API(1058): Google Play services package version: 4324036
05-02 07:33:32.267: D/dalvikvm(1058): GC_FOR_ALLOC freed 235K, 9% free 3150K/3452K, paused 27ms, total 28ms
05-02 07:33:32.357: D/AndroidRuntime(1058): Shutting down VM
05-02 07:33:32.357: W/dalvikvm(1058): threadid=1: thread exiting with uncaught exception (group=0xb1ae4ba8)
05-02 07:33:32.377: E/AndroidRuntime(1058): FATAL EXCEPTION: main
05-02 07:33:32.377: E/AndroidRuntime(1058): Process: net.learn2develop.googlemaps, PID: 1058
05-02 07:33:32.377: E/AndroidRuntime(1058): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.learn2develop.googlemaps/net.learn2develop.googlemaps.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
05-02 07:33:32.377: E/AndroidRuntime(1058): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-02 07:33:32.377: E/AndroidRuntime(1058): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-02 07:33:32.377: E/AndroidRuntime(1058): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-02 07:33:32.377: E/AndroidRuntime(1058): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-02 07:33:32.377: E/AndroidRuntime(1058): at android.os.Handler.dispatchMessage(Handler.java:102)
05-02 07:33:32.377: E/AndroidRuntime(1058): at android.os.Looper.loop(Looper.java:136)
05-02 07:33:32.377: E/AndroidRuntime(1058): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-02 07:33:32.377: E/AndroidRuntime(1058): at java.lang.reflect.Method.invokeNative(Native Method)
05-02 07:33:32.377: E/AndroidRuntime(1058): at java.lang.reflect.Method.invoke(Method.java:515)
05-02 07:33:32.377: E/AndroidRuntime(1058): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-02 07:33:32.377: E/AndroidRuntime(1058): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-02 07:33:32.377: E/AndroidRuntime(1058): at dalvik.system.NativeStart.main(Native Method)
05-02 07:33:32.377: E/AndroidRuntime(1058): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
05-02 07:33:32.377: E/AndroidRuntime(1058): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
05-02 07:33:32.377: E/AndroidRuntime(1058): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
05-02 07:33:32.377: E/AndroidRuntime(1058): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
05-02 07:33:32.377: E/AndroidRuntime(1058): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
05-02 07:33:32.377: E/AndroidRuntime(1058): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
05-02 07:33:32.377: E/AndroidRuntime(1058): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
05-02 07:33:32.377: E/AndroidRuntime(1058): at android.app.Activity.setContentView(Activity.java:1929)
05-02 07:33:32.377: E/AndroidRuntime(1058): at net.learn2develop.googlemaps.MainActivity.onCreate(MainActivity.java:32)
05-02 07:33:32.377: E/AndroidRuntime(1058): at android.app.Activity.performCreate(Activity.java:5231)
05-02 07:33:32.377: E/AndroidRuntime(1058): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-02 07:33:32.377: E/AndroidRuntime(1058): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-02 07:33:32.377: E/AndroidRuntime(1058): ... 11 more
05-02 07:33:32.377: E/AndroidRuntime(1058): Caused by: java.lang.SecurityException: The Maps API requires the additional following permissions to be set in the AndroidManifest.xml to ensure a correct behavior:
05-02 07:33:32.377: E/AndroidRuntime(1058): <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
05-02 07:33:32.377: E/AndroidRuntime(1058): at maps.e.ci.a(Unknown Source)
05-02 07:33:32.377: E/AndroidRuntime(1058): at maps.e.ay.a(Unknown Source)
05-02 07:33:32.377: E/AndroidRuntime(1058): at maps.e.ay.a(Unknown Source)
05-02 07:33:32.377: E/AndroidRuntime(1058): at maps.e.al.a(Unknown Source)
05-02 07:33:32.377: E/AndroidRuntime(1058): at maps.e.bh.a(Unknown Source)
05-02 07:33:32.377: E/AndroidRuntime(1058): at maps.e.bg.a(Unknown Source)
05-02 07:33:32.377: E/AndroidRuntime(1058): at etu.onTransact(SourceFile:107)
05-02 07:33:32.377: E/AndroidRuntime(1058): at android.os.Binder.transact(Binder.java:361)
05-02 07:33:32.377: E/AndroidRuntime(1058): at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown Source)
05-02 07:33:32.377: E/AndroidRuntime(1058): at com.google.android.gms.maps.SupportMapFragment$a.onCreateView(Unknown Source)
05-02 07:33:32.377: E/AndroidRuntime(1058): at com.google.android.gms.dynamic.a$4.b(Unknown Source)
05-02 07:33:32.377: E/AndroidRuntime(1058): at com.google.android.gms.dynamic.a.a(Unknown Source)
05-02 07:33:32.377: E/AndroidRuntime(1058): at com.google.android.gms.dynamic.a.onCreateView(Unknown Source)
05-02 07:33:32.377: E/AndroidRuntime(1058): at com.google.android.gms.maps.SupportMapFragment.onCreateView(Unknown Source)
05-02 07:33:32.377: E/AndroidRuntime(1058): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
05-02 07:33:32.377: E/AndroidRuntime(1058): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:884)
05-02 07:33:32.377: E/AndroidRuntime(1058): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1066)
05-02 07:33:32.377: E/AndroidRuntime(1058): at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1168)
05-02 07:33:32.377: E/AndroidRuntime(1058): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:280)
05-02 07:33:32.377: E/AndroidRuntime(1058): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
05-02 07:33:32.377: E/AndroidRuntime(1058): ... 21 more
答案 0 :(得分:0)
05-02 07:33:32.377:E / AndroidRuntime(1058):引起: java.lang.SecurityException:Maps API需要额外的 以下权限在AndroidManifest.xml中设置以确保 正确行为:05-02 07:33:32.377:E / AndroidRuntime(1058):
你错过了
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE"/>
清单文件中的
你有
android:minSdkVersion="19"
您需要更改此
public class MainActivity extends FragmentActivity {
要 公共类MainActivity扩展了Activity {
您需要使用MapFragment
class="com.google.android.gms.maps.MapFragment"
还需要改变
map = ((MapFragment) getFragmentManager()
.findFragmentById(R.id.map)).getMap();
你也不需要
<permission
android:name="net.learn2develop.googlemaps.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="net.learn2develop.googlemaps.permission.MAPS_RECEIVE" />