这里映射PositioningManager.getInstance()单例null对象?

时间:2016-02-22 09:59:25

标签: android here-api

问题:ProjectManager.getInstance();给出了null对象,我无能为力,我已完成下面的代码,

活动课程&清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="proj.Mobi">
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:hardwareAccelerated="true"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <meta-data android:name="com.here.android.maps.appid" android:value="WcIOjXKhgYk8" />
        <meta-data android:name="com.here.android.maps.apptoken" android:value="XaRs1SILAVi2sw" />
        <meta-data android:name="com.here.android.maps.license.key" android:value="phEpi5ZL3jiNTElgM3ldzrJTeevoNeYY47oE9eSzgJfySv=" />

    </application>

</manifest>

我正在创建下面的地图活动类,我创建了PositioningManager.getInstance()它通过Toast popup提供了null对象

public class MainActivity extends AppCompatActivity {
    // map embedded in the map fragment
    private Map map = null;
    // map fragment embedded in this activity
    private MapFragment mapFragment = null;

    private PositioningManager positioningManager = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Positioning Manager
        positioningManager = PositioningManager.getInstance();
        if (positioningManager == null) {
            Toast.makeText(MainActivity.this, " Positioning Null", Toast.LENGTH_LONG).show();
        }
        // Search for the map fragment to finish setup by calling init().
        mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.mapfragment);
        mapFragment.init(new OnEngineInitListener() {
            @Override
            public void onEngineInitializationCompleted(
                    OnEngineInitListener.Error error) {
                if (error == OnEngineInitListener.Error.NONE) {
                    // retrieve a reference of the map from the map fragment
                    map = mapFragment.getMap();
                    // Set the map center to the Vancouver region (no animation)
                    map.setCenter(new GeoCoordinate(12.9172, 77.5738, 0.0),
                            Map.Animation.NONE);
                    // Set the zoom level to the average between min and max
                    map.setZoomLevel(
                            (map.getMaxZoomLevel() + map.getMinZoomLevel()) / 2);
                } else {
                    System.out.println("ERROR: Cannot initialize Map Fragment");
                    Toast.makeText(MainActivity.this, "Error:" + error.toString(), Toast.LENGTH_LONG).show();
                }
            }
        });
    }

}

1 个答案:

答案 0 :(得分:1)

此处的所有内容地图相关需要在初始化后发生。这适用于与HERE MobileSDK相关的所有内容。

尝试在EngineInitListener.onEngineInitializationCompleted中获取PositioningManager(请记住,一旦获得实例,您还必须调用start来接收任何事件)