MapFragment易于初始化Here SDK

时间:2018-06-08 21:10:05

标签: android android-fragments sdk

尝试使用Here SDK创建简单的MapFragment时出现此错误。我从他们的网站上获取了大部分代码,但我在mapFragment.init()行上得到了一个空引用。起初我认为认证是错误的,但我在manifest检查过几次并且看起来都很好。 我正在使用免费的基本版本,如果这有所作为。

MainActivity.java

    package com.parallel.parallel;

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;

    import android.Manifest;
    import android.app.Activity;
    import android.content.pm.PackageManager;
    import android.os.Bundle;
    import android.support.annotation.NonNull;
    import android.support.v4.app.ActivityCompat;
    import android.support.v4.content.ContextCompat;
    import android.widget.TextView;
    import android.widget.Toast;
    import android.util.Log;

    import com.here.android.mpa.common.GeoCoordinate;
    import com.here.android.mpa.common.OnEngineInitListener;
    import com.here.android.mpa.mapping.Map;
    import com.here.android.mpa.mapping.MapFragment;

    public class MainActivity extends Activity {
      private static final String LOG_TAG = MainActivity.class.getSimpleName();

    // permissions request code
    private final static int REQUEST_CODE_ASK_PERMISSIONS = 1;

    /**
     * Permissions that need to be explicitly requested from end user.
     */
    private static final String[] REQUIRED_SDK_PERMISSIONS = new String[]{
            Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.WRITE_EXTERNAL_STORAGE};

    // map embedded in the map fragment
    private Map map = null;

    // map fragment embedded in this activity
    private MapFragment mapFragment = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        checkPermissions();
    }

    private void initialize(){
        setContentView(R.layout.activity_main);
        // 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(49.196261, -123.004773, 0.0),
                            Map.Animation.NONE);
                    // Set the zoom level to the average between min and max
                    map.setZoomLevel((map.getMaxZoomLevel() + map.getMinZoomLevel()) / 2);
                } else {
                    Log.e(LOG_TAG, "Cannot initialize MapFragment (" + error + ")");
                }
            }
        });
    }


    protected void checkPermissions() {
        final List<String> missingPermissions = new ArrayList<String>();
        // check all required dynamic permissions
        for (final String permission : REQUIRED_SDK_PERMISSIONS) {
            final int result = ContextCompat.checkSelfPermission(this, permission);
            if (result != PackageManager.PERMISSION_GRANTED) {
                missingPermissions.add(permission);
            }
        }
        if (!missingPermissions.isEmpty()) {
            // request all missing permissions
            final String[] permissions = missingPermissions
                    .toArray(new String[missingPermissions.size()]);
            ActivityCompat.requestPermissions(this, permissions, REQUEST_CODE_ASK_PERMISSIONS);
        } else {
            final int[] grantResults = new int[REQUIRED_SDK_PERMISSIONS.length];
            Arrays.fill(grantResults, PackageManager.PERMISSION_GRANTED);
            onRequestPermissionsResult(REQUEST_CODE_ASK_PERMISSIONS, REQUIRED_SDK_PERMISSIONS,
                    grantResults);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[],
                                           @NonNull int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CODE_ASK_PERMISSIONS:
                for (int index = permissions.length - 1; index >= 0; --index) {
                    if (grantResults[index] != PackageManager.PERMISSION_GRANTED) {
                        // exit the app if one permission is not granted
                        Toast.makeText(this, "Required permission '" + permissions[index]
                                + "' not granted, exiting", Toast.LENGTH_LONG).show();
                        finish();
                        return;
                    }
                }
                // all permissions were granted
                initialize();
                break;
        }
    }
}

文件的布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <fragment
        android:id="@+id/mapfragment"
        class="com.here.android.mpa.mapping.MapFragment"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

堆栈跟踪:

06-08 21:06:22.767 5079-5079/com.parallel.parallel E/InputEventReceiver: Exception dispatching input event.
06-08 21:06:22.767 5079-5079/com.parallel.parallel E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
06-08 21:06:22.768 5079-5079/com.parallel.parallel E/MessageQueue-JNI: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.view.GestureDetector.onTouchEvent(android.view.MotionEvent)' on a null object reference
        at com.here.android.mpa.internal.bs.onTouchEvent(MapViewImpl.java:300)
        at android.view.View.dispatchTouchEvent(View.java:11725)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2955)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2636)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2961)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2593)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2961)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2593)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2961)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2593)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2961)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2593)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2961)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2593)
        at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:445)
        at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1828)
        at android.app.Activity.dispatchTouchEvent(Activity.java:3292)
        at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:407)
        at android.view.View.dispatchPointerEvent(View.java:11964)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4776)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4590)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4128)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4181)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4147)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4274)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4155)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4331)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4128)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4181)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4147)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4155)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4128)
        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6642)
        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6616)
        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6577)
        at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6745)
        at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:325)
        at android.os.Looper.loop(Looper.java:142)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

0 个答案:

没有答案