我在课堂上有这个
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.coffeeandcookies.ushuaiamovil.R;
public class LayMapa2 extends FragmentActivity
{
@Override
protected void onCreate(Bundle arg0)
{
setContentView(R.layout.lay_mapa2);
super.onCreate(arg0);
}
}
这是我的xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment" />
这是我最明显的
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-feature android:glEsVersion="0x00020000"
android:required="true"/>
<permission
android:name="com.coffeeandcookies.usuahiamovil.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="com.coffeeandcookies.usuahiamovil.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<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.GET_ACCOUNTS" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="saasadsad" />
我在我的工作区中使用google-play-services_lib作为库proyect和android-support-v4.jar
这是我的logcat
07-17 11:46:10.945: E/AndroidRuntime(8241): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.coffeeandcookies.ushuaiamovil/com.coffeeandcookies.vistas.LayMapa2}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
07-17 11:46:10.945: E/AndroidRuntime(8241): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
07-17 11:46:10.945: E/AndroidRuntime(8241): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122)
07-17 11:46:10.945: E/AndroidRuntime(8241): at android.app.ActivityThread.access$600(ActivityThread.java:140)
07-17 11:46:10.945: E/AndroidRuntime(8241): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
07-17 11:46:10.945: E/AndroidRuntime(8241): at android.os.Handler.dispatchMessage(Handler.java:99)
07-17 11:46:10.945: E/AndroidRuntime(8241): at android.os.Looper.loop(Looper.java:137)
07-17 11:46:10.945: E/AndroidRuntime(8241): at android.app.ActivityThread.main(ActivityThread.java:4895)
07-17 11:46:10.945: E/AndroidRuntime(8241): at java.lang.reflect.Method.invokeNative(Native Method)
07-17 11:46:10.945: E/AndroidRuntime(8241): at java.lang.reflect.Method.invoke(Method.java:511)
07-17 11:46:10.945: E/AndroidRuntime(8241): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
07-17 11:46:10.945: E/AndroidRuntime(8241): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
07-17 11:46:10.945: E/AndroidRuntime(8241): at dalvik.system.NativeStart.main(Native Method)
07-17 11:46:10.945: E/AndroidRuntime(8241): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
07-17 11:46:10.945: E/AndroidRuntime(8241): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
07-17 11:46:10.945: E/AndroidRuntime(8241): at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
07-17 11:46:10.945: E/AndroidRuntime(8241): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-17 11:46:10.945: E/AndroidRuntime(8241): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
07-17 11:46:10.945: E/AndroidRuntime(8241): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:306)
07-17 11:46:10.945: E/AndroidRuntime(8241): at android.app.Activity.setContentView(Activity.java:1912)
07-17 11:46:10.945: E/AndroidRuntime(8241): at com.coffeeandcookies.vistas.LayMapa2.onCreate(LayMapa2.java:14)
07-17 11:46:10.945: E/AndroidRuntime(8241): at android.app.Activity.performCreate(Activity.java:5163)
07-17 11:46:10.945: E/AndroidRuntime(8241): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
07-17 11:46:10.945: E/AndroidRuntime(8241): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2061)
07-17 11:46:10.945: E/AndroidRuntime(8241): ... 11 more
07-17 11:46:10.945: E/AndroidRuntime(8241): Caused by: java.lang.ClassCastException: com.google.android.gms.maps.SupportMapFragment cannot be cast to android.app.Fragment
07-17 11:46:10.945: E/AndroidRuntime(8241): at android.app.Fragment.instantiate(Fragment.java:577)
07-17 11:46:10.945: E/AndroidRuntime(8241): at android.app.Fragment.instantiate(Fragment.java:552)
07-17 11:46:10.945: E/AndroidRuntime(8241): at android.app.Activity.onCreateView(Activity.java:4806)
07-17 11:46:10.945: E/AndroidRuntime(8241): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
07-17 11:46:10.945: E/AndroidRuntime(8241): ... 20 more
感谢!!!
答案 0 :(得分:7)
首先切换super.onCreate,然后切换setContentView。
这是因为super.onCreate调用框架的Activity.onCreate来分配扩展任何布局所需的资源。因此,必须在膨胀任何布局之前调用,xml / programmatic。
答案 1 :(得分:1)
更改您的代码:
public class MainActivity extends FragmentActivity {
private GoogleMap map;
private LatLng ltlng;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
答案 2 :(得分:0)
检查Bhavesh Patadiya的答案,https://stackoverflow.com/a/18227605/2684720
当您扩展FragmentActivity时,表明您正在使用与较低版本的android兼容的支持库v4。将MapFragment替换为xml文件中的SupportMapFragment。 SupportMapFragment是与Android Support包一起使用的。 MapFragment用于本机API Level 11版本的片段。