我已经从我的主项目中单独完成了一个简单的地图活动,它工作正常,但是当地图活动实施到主项目+尝试启动活动时,应用程序崩溃任何想法?这是地图活动代码+ logcat。
public class MainActivity extends Activity implements OnClickListener,
OnMapLongClickListener {
GoogleMap googleMap;
Location location;
Marker marker;
private void initilizeVars() {
googleMap.setOnMapLongClickListener(this);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
// Loading map
initilizeMap();
initilizeVars();
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* function to load map. If map is not created it will create it for you
* */
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
@Override
protected void onResume() {
super.onResume();
initilizeMap();
}
public void onMapLongClick(LatLng point) {
marker = googleMap.addMarker(new MarkerOptions()
.position(point)
.title("Marker")
.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
}
**MAP XML:**
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
logcat的:
03-20 21:36:17.116: E/AndroidRuntime(7080): FATAL EXCEPTION: main
03-20 21:36:17.116: E/AndroidRuntime(7080): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.attack.android/com.attack.android.Map}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
03-20 21:36:17.116: E/AndroidRuntime(7080): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2092)
03-20 21:36:17.116: E/AndroidRuntime(7080): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
03-20 21:36:17.116: E/AndroidRuntime(7080): at android.app.ActivityThread.access$700(ActivityThread.java:134)
03-20 21:36:17.116: E/AndroidRuntime(7080): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
03-20 21:36:17.116: E/AndroidRuntime(7080): at android.os.Handler.dispatchMessage(Handler.java:99)
03-20 21:36:17.116: E/AndroidRuntime(7080): at android.os.Looper.loop(Looper.java:137)
03-20 21:36:17.116: E/AndroidRuntime(7080): at android.app.ActivityThread.main(ActivityThread.java:4867)
03-20 21:36:17.116: E/AndroidRuntime(7080): at java.lang.reflect.Method.invokeNative(Native Method)
03-20 21:36:17.116: E/AndroidRuntime(7080): at java.lang.reflect.Method.invoke(Method.java:511)
03-20 21:36:17.116: E/AndroidRuntime(7080): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
03-20 21:36:17.116: E/AndroidRuntime(7080): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
03-20 21:36:17.116: E/AndroidRuntime(7080): at dalvik.system.NativeStart.main(Native Method)
03-20 21:36:17.116: E/AndroidRuntime(7080): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
03-20 21:36:17.116: E/AndroidRuntime(7080): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
03-20 21:36:17.116: E/AndroidRuntime(7080): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
03-20 21:36:17.116: E/AndroidRuntime(7080): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
03-20 21:36:17.116: E/AndroidRuntime(7080): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
03-20 21:36:17.116: E/AndroidRuntime(7080): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
03-20 21:36:17.116: E/AndroidRuntime(7080): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:316)
03-20 21:36:17.116: E/AndroidRuntime(7080): at android.app.Activity.setContentView(Activity.java:1901)
03-20 21:36:17.116: E/AndroidRuntime(7080): at com.attack.android.Map.onCreate(Map.java:42)
03-20 21:36:17.116: E/AndroidRuntime(7080): at android.app.Activity.performCreate(Activity.java:5047)
03-20 21:36:17.116: E/AndroidRuntime(7080): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
03-20 21:36:17.116: E/AndroidRuntime(7080): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2056)
03-20 21:36:17.116: E/AndroidRuntime(7080): ... 11 more
03-20 21:36:17.116: E/AndroidRuntime(7080): 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:
03-20 21:36:17.116: E/AndroidRuntime(7080): <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
03-20 21:36:17.116: E/AndroidRuntime(7080): at maps.e.ch.a(Unknown Source)
03-20 21:36:17.116: E/AndroidRuntime(7080): at maps.e.ay.a(Unknown Source)
03-20 21:36:17.116: E/AndroidRuntime(7080): at maps.e.ay.a(Unknown Source)
03-20 21:36:17.116: E/AndroidRuntime(7080): at maps.e.al.a(Unknown Source)
03-20 21:36:17.116: E/AndroidRuntime(7080): at maps.e.bg.a(Unknown Source)
03-20 21:36:17.116: E/AndroidRuntime(7080): at maps.e.bf.a(Unknown Source)
03-20 21:36:17.116: E/AndroidRuntime(7080): at eio.onTransact(SourceFile:107)
03-20 21:36:17.116: E/AndroidRuntime(7080): at android.os.Binder.transact(Binder.java:326)
03-20 21:36:17.116: E/AndroidRuntime(7080): at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown Source)
03-20 21:36:17.116: E/AndroidRuntime(7080): at com.google.android.gms.maps.MapFragment$a.onCreateView(Unknown Source)
03-20 21:36:17.116: E/AndroidRuntime(7080): at com.google.android.gms.dynamic.a$4.b(Unknown Source)
03-20 21:36:17.116: E/AndroidRuntime(7080): at com.google.android.gms.dynamic.a.a(Unknown Source)
03-20 21:36:17.116: E/AndroidRuntime(7080): at com.google.android.gms.dynamic.a.onCreateView(Unknown Source)
03-20 21:36:17.116: E/AndroidRuntime(7080): at com.google.android.gms.maps.MapFragment.onCreateView(Unknown Source)
03-20 21:36:17.116: E/AndroidRuntime(7080): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:807)
03-20 21:36:17.116: E/AndroidRuntime(7080): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1013)
03-20 21:36:17.116: E/AndroidRuntime(7080): at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1112)
03-20 21:36:17.116: E/AndroidRuntime(7080): at android.app.Activity.onCreateView(Activity.java:4703)
03-20 21:36:17.116: E/AndroidRuntime(7080): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
03-20 21:36:17.116: E/AndroidRuntime(7080): ... 21 more
答案 0 :(得分:1)
下面:
03-20 21:36:17.116: E/AndroidRuntime(7080): 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:
03-20 21:36:17.116: E/AndroidRuntime(7080): <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
你的logcat抱怨缺少权限:
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
将其添加到AndroidManifest.xml以避免SecurityException