我是android新手。 我正在使用谷歌地图开发应用程序。但经过多次测试后,我遇到了同样的问题:应用程序意外停止。
我提供代码。
The Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.map_project"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="21" />
<permission
android:name="com.map_project.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="com.map_project.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=".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="MyKeyxxx" />
</application>
</manifest>
map_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>
MainActivity:
package com.map_project;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
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.model.LatLng;
public class MainActivity extends FragmentActivity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.map_activity);
GoogleMap map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
LatLng sydney = new LatLng(-33.867, 151.206);
map.setMyLocationEnabled(true);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));
}
}
当我在手机上运行应用程序时,我收到消息:不幸的是,MyApp已经停止了。 如果有人能帮助我,那就太好了因为我开始绝望了。
答案 0 :(得分:0)
首先我想知道你是在Emulator或Android上测试它吗? 移动?
如果您在Emulator
,确保创建基于Google API的模拟器。 此模拟器还可用于测试Google地图和其他Google Play服务集成。
1)如果使用FragmentManager而不是SupportFragmentManager,则可以 并导入相同的!
2)在onCreate方法之前尝试声明static final LatLng sydney = new LatLng(-33,151);
3)Instead of Extending FragmentActivity you can extend Activity
您的代码看起来不错! 但您可以重新检查有关API密钥的这些步骤:
4)确保您已在GoogleAPI控制台中正确创建了API密钥,并且还激活了Google Maps Android API。(Google控制台 - &gt;服务 - &gt;打开它们)
使用 Try-Catch ,它可以帮助您追踪错误
try {
if (googleMap == null)
{
googleMap = ((MapFragment) getFragmentManager().
findFragmentById(R.id.map)).getMap();
}
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
Marker syd = googleMap.addMarker(new MarkerOptions().
position(sydney).title("Its Sydney"));
} catch (Exception e)
{
e.printStackTrace();
}