GetSupportFragmentManager始终返回null

时间:2013-10-05 17:48:35

标签: android google-maps mapfragment

我正在开发一个小型的Android应用程序,每当我调用GoogleMap map = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap()时,它总是返回null。 我正在尝试使用带有Google Play的API 17在设备上运行此应用程序。

MapActivity:

public class MapActivity extends FragmentActivity implements LocationSource, LocationListener {

    private static final String TAG = "MapActivity";
    private String device = android.os.Build.MODEL;

    private static final String UPDATE_LOC_URL = "http://lab.uzlabina.cz/~vongrad/Tracker/update_loc.php";
    private static final String LOAD_LOCATIONS = "http://lab.uzlabina.cz/~vongrad/Tracker/load_locations.php";
    private static final int ADD_MAP_PIN = 1;

    private Controller controller;

    private LocationManager locationManager;
    private OnLocationChangedListener locListener;


    private GoogleMap map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        controller = Controller.getInstance(new User());
        controller.setUserName(device);

        locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);

        if (locationManager != null){
            boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
            Log.d(TAG, "GPS " + isGPSEnabled + "Net " + isNetworkEnabled);

            if (isGPSEnabled){
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 50L, 1F, this);
            }
            else if (isNetworkEnabled){
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 500L, 10F, this);
            }
            else{
                Toast.makeText(this, "GPS is disabled", Toast.LENGTH_LONG);
            }
        }
        else
        {
            Toast.makeText(this, "Something went wrrong", Toast.LENGTH_LONG);
        }
        setUpMapIfNeeded(); 




    }
    public void onResume(){
        //updateLocation.start();
        super.onResume();
        //setUpMapIfNeeded();

        if (locationManager != null){
            map.setMyLocationEnabled(true);
        }
    }

    public void onPause(){
        //loadFriendsLocations.stop();
        if (locationManager != null){
            locationManager.removeUpdates(this);
        }
        super.onPause();
    }


    private void setUpMapIfNeeded() {
        if (map == null){

            map = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
            map.setLocationSource(this);

            if (map != null){
                setUpMap();
            }
        }
    }


    private void setUpMap() {
        map.setMyLocationEnabled(true); 
        loadFriendsLocations.start();
    }

布局:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MapActivity" >

    <TextView
        android:id="@+id/header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

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


</RelativeLayout>

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cz.vongrad.locator"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="17" />

    <permission
        android:name="cz.vongrad.locator.permission.MAPS_RECEIVE"
        android:protectionLevel="signature"/>
    <uses-permission android:name="cz.vongrad.locator.permission.MAPS_RECEIVE"/>


    <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" android:debuggable="true">

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="blabla"/>

        <activity
            android:name="cz.vongrad.locator.MapActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

logcat的:

   10-05 19:40:13.284: E/AndroidRuntime(2697): FATAL EXCEPTION: main
10-05 19:40:13.284: E/AndroidRuntime(2697): java.lang.RuntimeException: Unable to start activity ComponentInfo{cz.vongrad.locator/cz.vongrad.locator.MapActivity}: java.lang.NullPointerException
10-05 19:40:13.284: E/AndroidRuntime(2697):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
10-05 19:40:13.284: E/AndroidRuntime(2697):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
10-05 19:40:13.284: E/AndroidRuntime(2697):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-05 19:40:13.284: E/AndroidRuntime(2697):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
10-05 19:40:13.284: E/AndroidRuntime(2697):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-05 19:40:13.284: E/AndroidRuntime(2697):     at android.os.Looper.loop(Looper.java:137)
10-05 19:40:13.284: E/AndroidRuntime(2697):     at android.app.ActivityThread.main(ActivityThread.java:5041)
10-05 19:40:13.284: E/AndroidRuntime(2697):     at java.lang.reflect.Method.invokeNative(Native Method)
10-05 19:40:13.284: E/AndroidRuntime(2697):     at java.lang.reflect.Method.invoke(Method.java:511)
10-05 19:40:13.284: E/AndroidRuntime(2697):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-05 19:40:13.284: E/AndroidRuntime(2697):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-05 19:40:13.284: E/AndroidRuntime(2697):     at dalvik.system.NativeStart.main(Native Method)
10-05 19:40:13.284: E/AndroidRuntime(2697): Caused by: java.lang.NullPointerException
10-05 19:40:13.284: E/AndroidRuntime(2697):     at cz.vongrad.locator.MapActivity.setUpMapIfNeeded(MapActivity.java:115)
10-05 19:40:13.284: E/AndroidRuntime(2697):     at cz.vongrad.locator.MapActivity.onCreate(MapActivity.java:86)
10-05 19:40:13.284: E/AndroidRuntime(2697):     at android.app.Activity.performCreate(Activity.java:5104)
10-05 19:40:13.284: E/AndroidRuntime(2697):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
10-05 19:40:13.284: E/AndroidRuntime(2697):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
10-05 19:40:13.284: E/AndroidRuntime(2697):     ... 11 more

我将不胜感激任何建议。 提前谢谢!

1 个答案:

答案 0 :(得分:0)

在初始化locationmanager之前尝试尽早调用这个setUpMapIfNeeded()几行

setContentView(R.layout.activity_main);

        controller = Controller.getInstance(new User());
        controller.setUserName(device);

 setUpMapIfNeeded();