我一直在試圖擴展FragmentActivity。如果我將MainActivity類擴展到Activity併爲正常Activity導入必需的包,則該應用程序可以工作。如果我擴展FragmentActivity,應用程序崩潰
基本需求是我無法在正常活動中訪問地圖。它需要擴展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>
這裏是LOG
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
該應用程序在上述情況下崩潰。 –
你的android:minSdkVersion是11.你不需要FragmentActivity,只需使用Activity。根據http://developer.android.com/reference/android/support/v4/app/FragmentActivity。html: 「希望使用基於支持的Fragment和Loader API的活動的基類」。 在這種情況下,您不需要支持庫。 –