我是Android上的新手,我试图将谷歌地图与我的应用程序集成,我也尝试了vogella和androidhive,并看到堆栈上的问题和答案溢出但这里没有任何作品是android代码
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; //10 metters
//The minimum time beetwen updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.mainscreen);
// .findFragmentById(R.id.map))
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
if (map!=null){
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)));
}
}
这是xml代码
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
在这里我有例外,请告诉我我错在哪里
01-21 23:26:47.304: E/AndroidRuntime(1456): FATAL EXCEPTION: main
01-21 23:26:47.304: E/AndroidRuntime(1456): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.geekdev.animalpark/com.geekdev.animalpark.MainScreen}: android.view.InflateException: Binary XML file line #54: Error inflating class fragment
01-21 23:26:47.304: E/AndroidRuntime(1456): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-21 23:26:47.304: E/AndroidRuntime(1456): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-21 23:26:47.304: E/AndroidRuntime(1456): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-21 23:26:47.304: E/AndroidRuntime(1456): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-21 23:26:47.304: E/AndroidRuntime(1456): at android.os.Handler.dispatchMessage(Handler.java:99)
01-21 23:26:47.304: E/AndroidRuntime(1456): at android.os.Looper.loop(Looper.java:137)
01-21 23:26:47.304: E/AndroidRuntime(1456): at android.app.ActivityThread.main(ActivityThread.java:5041)
01-21 23:26:47.304: E/AndroidRuntime(1456): at java.lang.reflect.Method.invokeNative(Native Method)
01-21 23:26:47.304: E/AndroidRuntime(1456): at java.lang.reflect.Method.invoke(Method.java:511)
01-21 23:26:47.304: E/AndroidRuntime(1456): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-21 23:26:47.304: E/AndroidRuntime(1456): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-21 23:26:47.304: E/AndroidRuntime(1456): at dalvik.system.NativeStart.main(Native Method)
01-21 23:26:47.304: E/AndroidRuntime(1456): Caused by: android.view.InflateException: Binary XML file line #54: Error inflating class fragment
01-21 23:26:47.304: E/AndroidRuntime(1456): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
01-21 23:26:47.304: E/AndroidRuntime(1456): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
01-21 23:26:47.304: E/AndroidRuntime(1456): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
01-21 23:26:47.304: E/AndroidRuntime(1456): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
01-21 23:26:47.304: E/AndroidRuntime(1456): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-21 23:26:47.304: E/AndroidRuntime(1456): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-21 23:26:47.304: E/AndroidRuntime(1456): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
01-21 23:26:47.304: E/AndroidRuntime(1456): at android.app.Activity.setContentView(Activity.java:1881)
01-21 23:26:47.304: E/AndroidRuntime(1456): at com.geekdev.animalpark.MainScreen.onCreate(MainScreen.java:57)
01-21 23:26:47.304: E/AndroidRuntime(1456): at android.app.Activity.performCreate(Activity.java:5104)
01-21 23:26:47.304: E/AndroidRuntime(1456): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-21 23:26:47.304: E/AndroidRuntime(1456): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-21 23:26:47.304: E/AndroidRuntime(1456): ... 11 more
01-21 23:26:47.304: E/AndroidRuntime(1456): Caused by: java.lang.ClassCastException: com.google.android.gms.maps.SupportMapFragment cannot be cast to android.app.Fragment
01-21 23:26:47.304: E/AndroidRuntime(1456): at android.app.Fragment.instantiate(Fragment.java:585)
01-21 23:26:47.304: E/AndroidRuntime(1456): at android.app.Fragment.instantiate(Fragment.java:560)
01-21 23:26:47.304: E/AndroidRuntime(1456): at android.app.Activity.onCreateView(Activity.java:4709)
01-21 23:26:47.304: E/AndroidRuntime(1456): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="17" />
<permission
android:name="com.geekdev.animalpark.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="com.geekdev.animalpark.permission.MAPS_RECEIVE" />
<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" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<!-- Goolge Maps API Key -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyCSzy5IWRWvMUn8xt5I17Z-1l7xtzYM0Js" />
我已经尝试过我在互联网上找到的所有内容但是没有在这里工作请告诉我适当的解决方案请
答案 0 :(得分:0)
元数据应该是应用程序标记的一部分
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyCSzy5IWRWvMUn8xt5I17Z-1l7xtzYM0Js" />
编辑:
<application>
<activity
...
..
..
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="Yourkey" />
</application>