2013-06-22 171 views
1

我一直在試圖擴展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 
+0

該應用程序在上述情況下崩潰。 –

+0

你的android:minSdkVersion是11.你不需要FragmentActivity,只需使用Activity。根據http://developer.android.com/reference/android/support/v4/app/FragmentActivity。html: 「希望使用基於支持的Fragment和Loader API的活動的基類」。 在這種情況下,您不需要支持庫。 –

回答

0

你分鐘SDK是11.我想你可以把它12和使用MapFragment

要知道爲什麼檢查鏈接http://developer.android.com/about/dashboards/index.html

在這種情況下(minsdk是12歲或以上),你可以擴展Activity

GoogleMap mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)) 
           .getMap(); 

還進口

import com.google.android.gms.maps.MapFragment; 

        Or 

您應該使用Support Fragment

<fragment 
class="com.google.android.gms.maps.SupportMapFragment" 
android:id="@+id/map" 
android:layout_width="match_parent" 
android:layout_height="match_parent"/> 

您的活動必須擴展FragmentActivity

SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 
GoogleMap mMap = fm.getMap(); 

確保您已經添加支持庫

還要確保您導入下面的

import android.support.v4.app.FragmentActivity; 
import com.google.android.gms.maps.SupportMapFragment;  
+0

原來,我只好把片段放在線性/相對佈局中。無論如何感謝 –

+0

@ user2361490在任何情況下,如果你的最低sdk低於12你應該使用'SupportFrament'。請檢查鏈接https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/MapFragment。請查看開發人員指南標題 – Raghunandan

+0

@ user2361490上方的行,如果您使用'MapFragment',則可以擴展標準的「活動」標準。所以在這種情況下將你的min sdk改爲12或者按照建議使用'SupportFragment'。 – Raghunandan

0

這裏是佈局XML

<fragment

我不知道,如果你能有一個片段佈局的根的片段應該是被放在另一個佈局和交換片。

你有沒有嘗試把你的片段放在FrameLayout中,看看它是否仍然崩潰?

+0

「原來,我只是不得不將片段放在線性/相對佈局中,謝謝」。我想我應該在發佈之前刷新。 –

相關問題