getMapAsync()导致我的应用程序崩溃

时间:2017-05-30 23:14:50

标签: java android

我在这里搜索了同样的问题,但答案并没有解决我的问题 当我删除这一行时:

mapFragment.getMapAsync(this);

该应用有效,但会显示没有onMapReady()功能设置的常规地图。

Main_Screen.java:

import android.app.Dialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class Main_Screen extends AppCompatActivity implements 
OnMapReadyCallback {

private int PICK_CONTACT = -1; // to store a contact number
ImageButton addContactButton;
ImageButton menuButton;
MapView mapView;

GoogleMap mGoogleMap;

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

    SupportMapFragment mapFragment = (SupportMapFragment) 
    getSupportFragmentManager().findFragmentById(R.id.mapFragment);

    if(googleServicesAvailable()&& mapFragment != null){
        Toast.makeText(this,"Current location", Toast.LENGTH_LONG).show();
        mapFragment.getMapAsync(this); // this line cause crash

    }else{
        //No google maps layout
    }

    addContactButton = (ImageButton) findViewById(R.id.green); // add 
    contact button off
    //addContactButton.setOnClickListener(imgButtonHandler);
    menuButton = (ImageButton) findViewById(R.id.menuButtonoff); // add contact button off
    //menuButton.setImageResource(R.drawable.menuunpressed);

}

// check if the user have "google play services" on the device and if not notify
public boolean googleServicesAvailable(){
    GoogleApiAvailability api = GoogleApiAvailability.getInstance();
    int isAvailable = api.isGooglePlayServicesAvailable(this);

    if(isAvailable == ConnectionResult.SUCCESS){
        return true;
    }else if(api.isUserResolvableError(isAvailable)){
        Dialog dialog = api.getErrorDialog(this, isAvailable,0);
        dialog.show();
    }else{
        Toast.makeText(this,"Cant connect to play services", Toast.LENGTH_LONG).show();
    }
    return false;
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mGoogleMap = googleMap;
    goToLocationWithZoom(20.4123,25.232,10);
}

public void goToLocationWithZoom(double lat, double lng, float zoom){
    LatLng ll = new LatLng(lat,lng);
    CameraUpdate update = CameraUpdateFactory.newLatLngZoom(ll, zoom);
    mGoogleMap.addMarker(new MarkerOptions().title("Current location"));
    mGoogleMap.moveCamera(update);
}
}

main_screen.xml

        <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:layout_alignBottom="@+id/background"
        android:orientation="vertical">

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

日志:

05-31 02:47:15.364 5184-5184/? E/Zygote: v2
05-31 02:47:15.364 5184-5184/? I/libpersona: KNOX_SDCARD checking this for 10268
05-31 02:47:15.364 5184-5184/? I/libpersona: KNOX_SDCARD not a persona
05-31 02:47:15.367 5184-5184/? E/Zygote: accessInfo : 0
05-31 02:47:15.368 5184-5184/? W/SELinux: SELinux selinux_android_compute_policy_index : Policy Index[2],  Con:u:r:zygote:s0 RAM:SEPF_SECMOBILE_7.0_0005, [-1 -1 -1 -1 0 1]
05-31 02:47:15.372 5184-5184/? I/SELinux: SELinux: seapp_context_lookup: seinfo=untrusted, level=s0:c512,c768, pkgname=com.example.galaharonshmueluzan.way_whereareyou 
05-31 02:47:15.381 5184-5184/? I/art: Late-enabling -Xcheck:jni
05-31 02:47:15.416 5184-5184/? D/TimaKeyStoreProvider: TimaKeyStore is not enabled: cannot add TimaSignature Service and generateKeyPair Service
05-31 02:47:15.633 5184-5184/com.example.galaharonshmueluzan.way_whereareyou W/System: ClassLoader referenced unknown path: /data/app/com.example.galaharonshmueluzan.way_whereareyou-2/lib/arm64
05-31 02:47:15.650 5184-5184/com.example.galaharonshmueluzan.way_whereareyou I/InstantRun: starting instant run server: is main process
05-31 02:47:15.704 5184-5184/com.example.galaharonshmueluzan.way_whereareyou W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
05-31 02:47:15.959 5184-5184/com.example.galaharonshmueluzan.way_whereareyou D/ViewRootImpl@40cab45[Splash_Screen]: ThreadedRenderer.create() translucent=false
05-31 02:47:15.968 5184-5184/com.example.galaharonshmueluzan.way_whereareyou D/InputTransport: Input channel constructed: fd=71
05-31 02:47:15.974 5184-5184/com.example.galaharonshmueluzan.way_whereareyou D/ViewRootImpl@40cab45[Splash_Screen]: setView = DecorView@1f2229a[Splash_Screen] touchMode=true
05-31 02:47:15.983 5184-5184/com.example.galaharonshmueluzan.way_whereareyou D/ViewRootImpl@40cab45[Splash_Screen]: dispatchAttachedToWindow
05-31 02:47:16.020 5184-5184/com.example.galaharonshmueluzan.way_whereareyou D/ViewRootImpl@40cab45[Splash_Screen]: Relayout returned: oldFrame=[0,0][0,0] newFrame=[0,0][1440,2560] result=0x27 surface={isValid=true 502085821952} surfaceGenerationChanged=true
05-31 02:47:16.021 5184-5184/com.example.galaharonshmueluzan.way_whereareyou D/ViewRootImpl@40cab45[Splash_Screen]: mHardwareRenderer.initialize() mSurface={isValid=true 502085821952} hwInitialized=true
05-31 02:47:16.032 5184-5184/com.example.galaharonshmueluzan.way_whereareyou D/ViewRootImpl@40cab45[Splash_Screen]: MSG_RESIZED_REPORT: ci=Rect(0, 96 - 0, 0) vi=Rect(0, 96 - 0, 0) or=1
05-31 02:47:16.032 5184-5184/com.example.galaharonshmueluzan.way_whereareyou D/ViewRootImpl@40cab45[Splash_Screen]: MSG_WINDOW_FOCUS_CHANGED 1
05-31 02:47:16.033 5184-5184/com.example.galaharonshmueluzan.way_whereareyou D/ViewRootImpl@40cab45[Splash_Screen]: mHardwareRenderer.initializeIfNeeded()#2 mSurface={isValid=true 502085821952}
05-31 02:47:16.034 5184-5184/com.example.galaharonshmueluzan.way_whereareyou V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@dd1b6c1 nm : com.example.galaharonshmueluzan.way_whereareyou ic=null
05-31 02:47:16.034 5184-5184/com.example.galaharonshmueluzan.way_whereareyou I/InputMethodManager: [IMM] startInputInner - mService.startInputOrWindowGainedFocus
05-31 02:47:16.045 5184-5215/com.example.galaharonshmueluzan.way_whereareyou D/libEGL: loaded /vendor/lib64/egl/libGLES_mali.so
05-31 02:47:16.052 5184-5197/com.example.galaharonshmueluzan.way_whereareyou D/InputTransport: Input channel constructed: fd=72
05-31 02:47:16.080 5184-5215/com.example.galaharonshmueluzan.way_whereareyou I/OpenGLRenderer: Initialized EGL, version 1.4
05-31 02:47:16.080 5184-5215/com.example.galaharonshmueluzan.way_whereareyou D/OpenGLRenderer: Swap behavior 1
05-31 02:47:16.086 5184-5215/com.example.galaharonshmueluzan.way_whereareyou D/mali_winsys: EGLint new_window_surface(egl_winsys_display*, void*, EGLSurface, EGLConfig, egl_winsys_surface**, egl_color_buffer_format*, EGLBoolean) returns 0x3000,  [1440x2560]-format:1
05-31 02:47:16.266 5184-5184/com.example.galaharonshmueluzan.way_whereareyou V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@2fb87c0 nm : com.example.galaharonshmueluzan.way_whereareyou ic=null
05-31 02:47:16.266 5184-5184/com.example.galaharonshmueluzan.way_whereareyou W/IInputConnectionWrapper: reportFullscreenMode on inexistent InputConnection
05-31 02:47:17.757 5184-5184/com.example.galaharonshmueluzan.way_whereareyou D/ViewRootImpl@40cab45[Splash_Screen]: MSG_WINDOW_FOCUS_CHANGED 0
05-31 02:47:17.835 5184-5184/com.example.galaharonshmueluzan.way_whereareyou D/ViewRootImpl@40cab45[Splash_Screen]: mHardwareRenderer.destroy()#1
05-31 02:47:17.853 5184-5184/com.example.galaharonshmueluzan.way_whereareyou D/ViewRootImpl@40cab45[Splash_Screen]: Relayout returned: oldFrame=[0,0][1440,2560] newFrame=[0,0][1440,2560] result=0x5 surface={isValid=false 0} surfaceGenerationChanged=true
05-31 02:47:17.953 5184-5184/com.example.galaharonshmueluzan.way_whereareyou I/zzai: Making Creator dynamically
05-31 02:47:18.019 5184-5184/com.example.galaharonshmueluzan.way_whereareyou W/System: ClassLoader referenced unknown path: 
05-31 02:47:18.109 5184-5184/com.example.galaharonshmueluzan.way_whereareyou I/Google Maps Android API: Google Play services client version: 10260000
05-31 02:47:18.114 5184-5184/com.example.galaharonshmueluzan.way_whereareyou I/Google Maps Android API: Google Play services package version: 11055440
05-31 02:47:18.500 5184-5184/com.example.galaharonshmueluzan.way_whereareyou D/AbsListView: Get MotionRecognitionManager
05-31 02:47:18.502 5184-5184/com.example.galaharonshmueluzan.way_whereareyou D/MotionRecognitionManager: mSContextService = com.samsung.android.hardware.context.ISemContextService$Stub$Proxy@5024913
05-31 02:47:18.504 5184-5184/com.example.galaharonshmueluzan.way_whereareyou D/MotionRecognitionManager: motionService = com.samsung.android.gesture.IMotionRecognitionService$Stub$Proxy@bef1750
05-31 02:47:18.504 5184-5184/com.example.galaharonshmueluzan.way_whereareyou D/MotionRecognitionManager: motionService = com.samsung.android.gesture.IMotionRecognitionService$Stub$Proxy@bef1750
05-31 02:47:18.555 5184-5314/com.example.galaharonshmueluzan.way_whereareyou W/agl: copyMemory is missing from platform - proto runtime falling back to safer methods.
05-31 02:47:18.583 5184-5298/com.example.galaharonshmueluzan.way_whereareyou D/NetworkSecurityConfig: No Network Security Config specified, using platform default
05-31 02:47:18.596 5184-5298/com.example.galaharonshmueluzan.way_whereareyou I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
05-31 02:47:18.597 5184-5298/com.example.galaharonshmueluzan.way_whereareyou I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
05-31 02:47:18.930 5184-5184/com.example.galaharonshmueluzan.way_whereareyou D/ViewRootImpl@b529999[Main_Screen]: ThreadedRenderer.create() translucent=false
05-31 02:47:18.932 5184-5184/com.example.galaharonshmueluzan.way_whereareyou D/InputTransport: Input channel constructed: fd=114
05-31 02:47:18.932 5184-5184/com.example.galaharonshmueluzan.way_whereareyou D/ViewRootImpl@b529999[Main_Screen]: setView = DecorView@24e345e[Main_Screen] touchMode=true
05-31 02:47:18.934 5184-5184/com.example.galaharonshmueluzan.way_whereareyou I/Choreographer: Skipped 62 frames!  The application may be doing too much work on its main thread.
05-31 02:47:18.935 5184-5184/com.example.galaharonshmueluzan.way_whereareyou D/ViewRootImpl@b529999[Main_Screen]: dispatchAttachedToWindow
05-31 02:47:18.952 5184-5184/com.example.galaharonshmueluzan.way_whereareyou D/ViewRootImpl@b529999[Main_Screen]: Relayout returned: oldFrame=[0,0][0,0] newFrame=[0,0][1440,2560] result=0x27 surface={isValid=true 502085821952} surfaceGenerationChanged=true
05-31 02:47:18.952 5184-5184/com.example.galaharonshmueluzan.way_whereareyou D/ViewRootImpl@b529999[Main_Screen]: mHardwareRenderer.initialize() mSurface={isValid=true 502085821952} hwInitialized=true
05-31 02:47:18.957 5184-5215/com.example.galaharonshmueluzan.way_whereareyou D/mali_winsys: EGLint new_window_surface(egl_winsys_display*, void*, EGLSurface, EGLConfig, egl_winsys_surface**, egl_color_buffer_format*, EGLBoolean) returns 0x3000,  [1440x2560]-format:1
05-31 02:47:18.962 5184-5184/com.example.galaharonshmueluzan.way_whereareyou D/ViewRootImpl@40cab45[Splash_Screen]: mHardwareRenderer.destroy()#4
05-31 02:47:18.962 5184-5184/com.example.galaharonshmueluzan.way_whereareyou D/ViewRootImpl@40cab45[Splash_Screen]: dispatchDetachedFromWindow
05-31 02:47:18.966 5184-5184/com.example.galaharonshmueluzan.way_whereareyou D/InputTransport: Input channel destroyed: fd=71
05-31 02:47:18.987 5184-5184/com.example.galaharonshmueluzan.way_whereareyou D/ViewRootImpl@8758335[Toast]: ThreadedRenderer.create() translucent=true
05-31 02:47:18.989 5184-5184/com.example.galaharonshmueluzan.way_whereareyou D/InputTransport: Input channel constructed: fd=115
05-31 02:47:18.990 5184-5184/com.example.galaharonshmueluzan.way_whereareyou D/ViewRootImpl@8758335[Toast]: setView = android.widget.LinearLayout{f55d858 V.E...... ......I. 0,0-0,0} touchMode=true
05-31 02:47:19.014 5184-5184/com.example.galaharonshmueluzan.way_whereareyou D/AndroidRuntime: Shutting down VM


                                                                                               --------- beginning of crash
05-31 02:47:19.015 5184-5184/com.example.galaharonshmueluzan.way_whereareyou E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                               Process: com.example.galaharonshmueluzan.way_whereareyou, PID: 5184
                                                                                               java.lang.IllegalArgumentException: no position in marker options
                                                                                                   at com.google.maps.api.android.lib6.common.l.b(:com.google.android.gms.DynamiteModulesB:20)
                                                                                                   at com.google.maps.api.android.lib6.impl.az.a(:com.google.android.gms.DynamiteModulesB:481)
                                                                                                   at com.google.android.gms.maps.internal.j.onTransact(:com.google.android.gms.DynamiteModulesB:115)
                                                                                                   at android.os.Binder.transact(Binder.java:507)
                                                                                                   at com.google.android.gms.maps.internal.IGoogleMapDelegate$zza$zza.addMarker(Unknown Source)
                                                                                                   at com.google.android.gms.maps.GoogleMap.addMarker(Unknown Source)
                                                                                                   at com.example.galaharonshmueluzan.way_whereareyou.Main_Screen.goToLocationWithZoom(Main_Screen.java:84)
                                                                                                   at com.example.galaharonshmueluzan.way_whereareyou.Main_Screen.onMapReady(Main_Screen.java:78)
                                                                                                   at com.google.android.gms.maps.SupportMapFragment$zza$1.zza(Unknown Source)
                                                                                                   at com.google.android.gms.maps.internal.zzt$zza.onTransact(Unknown Source)
                                                                                                   at android.os.Binder.transact(Binder.java:507)
                                                                                                   at com.google.android.gms.maps.internal.aq.a(:com.google.android.gms.DynamiteModulesB:5)
                                                                                                   at com.google.maps.api.android.lib6.impl.bb.run(:com.google.android.gms.DynamiteModulesB:5)
                                                                                                   at android.os.Handler.handleCallback(Handler.java:751)
                                                                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                                   at android.os.Looper.loop(Looper.java:154)
                                                                                                   at android.app.ActivityThread.main(ActivityThread.java:6682)
                                                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
                                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
05-31 02:47:19.041 5184-5184/com.example.galaharonshmueluzan.way_whereareyou I/Process: Sending signal. PID: 5184 SIG: 9

2 个答案:

答案 0 :(得分:2)

编辑

Android - Google Maps Extensions - IllegalArgumentException

以下是代码问题的相关部分

at com.google.android.gms.maps.GoogleMap.addMarker(Unknown Source)
at com.example.galaharonshmueluzan.way_whereareyou.Main_Screen.goToLocationWithZoom(Main_Screen.java:84)

您没有在标记中添加位置

mGoogleMap.addMarker(new MarkerOptions().title("Current location"));

原始答案

尝试将setContentView(R.layout.main_screen);移到您尝试查找片段的位置上方。否则,它为null。

或者在if语句中更安全

setContentView(R.layout.main_screen);

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapFragment);

if(googleServicesAvailable() && mapFragment != null){
    mapFragment.getMapAsync(this); 
} else {
    // No Google Play Services
}

答案 1 :(得分:0)

尝试使用android的MapView而不是Fragment。由于向后兼容性,碎片可能会导致错误,并且可能在不同的设备上表现不同。

在您的XML中:

 <com.google.android.gms.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

在你的Java中:

onCreate(){
    mContext=getContext();
            View rootView = inflater.inflate(R.layout.fragment_pager_map, container, false);
            mMapView = (MapView) rootView.findViewById(R.id.mapView);
            mMapView.onCreate(savedInstanceState);

            mMapView.onResume(); // needed to get the map to display immediately
}