我一直在尝试扩展 FragmentActivity。如果我将 MainActivity 类扩展到 Activity 并为普通 Activity 导入必要的包,则相同的应用程序可以工作。
基本需求是我无法在正常活动中访问地图。它需要扩展 FragmentActivity 以运行以下行。
googleMap= ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
这是Java代码
package com.dacoders.locationtodo;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
这是布局 XML
<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.MapFragment"/>
这是 AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dacoders.locationtodo"
android:versionCode="1"
android:versionName="1.0" >
<permission
android:name="com.dacoders.locationtodo.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.dacoders.locationtodo.permission.MAPS_RECEIVE"/>
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- The following two permissions are not required to use
Google Maps Android API v2, but are recommended. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.dacoders.locationtodo.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="API_KEY_HERE"/>
</application>
</manifest>
这是日志
0622 21:01:25.911: D/dalvikvm(13621): Lateenabling CheckJNI
0622 21:01:25.991: D/AndroidRuntime(13621): Shutting down VM
0622 21:01:25.991: W/dalvikvm(13621): threadid=1: thread exiting with uncaught exception (group=0x41629930)
0622 21:01:26.001: E/AndroidRuntime(13621): FATAL EXCEPTION: main
0622 21:01:26.001: E/AndroidRuntime(13621): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dacoders.locationtodo/com.dacoders.locationtodo.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
0622 21:01:26.001: E/AndroidRuntime(13621): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
0622 21:01:26.001: E/AndroidRuntime(13621): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
0622 21:01:26.001: E/AndroidRuntime(13621): at android.app.ActivityThread.access$600(ActivityThread.java:141)
0622 21:01:26.001: E/AndroidRuntime(13621): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
0622 21:01:26.001: E/AndroidRuntime(13621): at android.os.Handler.dispatchMessage(Handler.java:99)
0622 21:01:26.001: E/AndroidRuntime(13621): at android.os.Looper.loop(Looper.java:137)
0622 21:01:26.001: E/AndroidRuntime(13621): at android.app.ActivityThread.main(ActivityThread.java:5041)
0622 21:01:26.001: E/AndroidRuntime(13621): at java.lang.reflect.Method.invokeNative(Native Method)
0622 21:01:26.001: E/AndroidRuntime(13621): at java.lang.reflect.Method.invoke(Method.java:511)
0622 21:01:26.001: E/AndroidRuntime(13621): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
0622 21:01:26.001: E/AndroidRuntime(13621): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
0622 21:01:26.001: E/AndroidRuntime(13621): at dalvik.system.NativeStart.main(Native Method)
0622 21:01:26.001: E/AndroidRuntime(13621): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
0622 21:01:26.001: E/AndroidRuntime(13621): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
0622 21:01:26.001: E/AndroidRuntime(13621): at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
0622 21:01:26.001: E/AndroidRuntime(13621): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
0622 21:01:26.001: E/AndroidRuntime(13621): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
0622 21:01:26.001: E/AndroidRuntime(13621): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
0622 21:01:26.001: E/AndroidRuntime(13621): at android.app.Activity.setContentView(Activity.java:1881)
0622 21:01:26.001: E/AndroidRuntime(13621): at com.dacoders.locationtodo.MainActivity.onCreate(MainActivity.java:12)
0622 21:01:26.001: E/AndroidRuntime(13621): at android.app.Activity.performCreate(Activity.java:5104)
0622 21:01:26.001: E/AndroidRuntime(13621): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
0622 21:01:26.001: E/AndroidRuntime(13621): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
0622 21:01:26.001: E/AndroidRuntime(13621): ... 11 more
0622 21:01:26.001: E/AndroidRuntime(13621): Caused by: java.lang.ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.support.v4.app.Fragment
0622 21:01:26.001: E/AndroidRuntime(13621): at android.support.v4.app.Fragment.instantiate(Fragment.java:394)
0622 21:01:26.001: E/AndroidRuntime(13621): at android.support.v4.app.Fragment.instantiate(Fragment.java:369)
0622 21:01:26.001: E/AndroidRuntime(13621): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
0622 21:01:26.001: E/AndroidRuntime(13621): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
0622 21:01:26.001: E/AndroidRuntime(13621): ... 20 more