当前位置:  开发笔记 > 编程语言 > 正文

尝试在空对象引用上调用虚拟方法'void com.google.android.gms.maps.GoogleMap.setOnMapLongClickListener'

如何解决《尝试在空对象引用上调用虚拟方法'voidcom.google.android.gms.maps.GoogleMap.setOnMapLongClickListener'》经验,为你挑选了1个好方法。

当我长按地图时,应在该位置添加标记。但是它不起作用。我尝试运行此应用时,我的应用崩溃了。请帮帮我

这是我的代码

package inandroid.jeetna.memorableplaces;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback,GoogleMap.OnMapLongClickListener {

    private GoogleMap mMap;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        mMap.setOnMapLongClickListener(this);

        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);


        Intent i = getIntent();
        Log.i("locationInfo", Integer.toString(i.getIntExtra("locationinfo", -1)));
    }



    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }


    @Override
    public void onMapLongClick(LatLng point) {
        mMap.addMarker(new MarkerOptions()
                .position(point)
                .title("You are here")
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
    }

}

这是我收到的错误消息

12-29 12:42:07.118 7519-7519 / inandroid.jeetna.memorableplaces E / AndroidRuntime:致命例外:主要12-29 12:42:07.118 7519-7519 / inandroid.jeetna.memorableplaces E / AndroidRuntime:进程:inandroid。 jeetna.memorableplaces,PID:7519 12-29 12:42:07.118 7519-7519 / inandroid.jeetna.memorableplaces E / AndroidRuntime:java.lang.RuntimeException:无法启动活动ComponentInfo {inandroid.jeetna.memorableplaces / inandroid.jeetna。 memorableplaces.MapsActivity}:java.lang.NullPointerException:尝试在null上调用虚拟方法“ void com.google.android.gms.maps.GoogleMap.setOnMapLongClickListener(com.google.android.gms.maps.GoogleMap $ OnMapLongClickListener)”对象参考12-29 12:42:07.118 7519-7519 / inandroid.jeetna.memorableplaces E / AndroidRuntime:位于android.app.ActivityThread。performLaunchActivity(ActivityThread.java:2298)12-29 12:42:07.118 7519-7519 / inandroid.jeetna.memorableplaces E / AndroidRuntime:at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)12-29 12:42 :07.118 7519-7519 / inandroid.jeetna.memorableplaces E / AndroidRuntime:at android.app.ActivityThread.access $ 800(ActivityThread.java:144)12-29 12:42:07.118 7519-7519 / inandroid.jeetna.memorableplaces E / AndroidRuntime:位于android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1278)12-29 12:42:07.118 7519-7519 / inandroid.jeetna.memorableplaces E / AndroidRuntime:位于android.os.Handler.dispatchMessage(Handler .java:102)12-29 12:42:07.118 7519-7519 / inandroid.jeetna.memorableplaces E / AndroidRuntime:at android.os.Looper.loop(Looper.java:135)12-29 12:42:07。118 7519-7519 / inandroid.jeetna.memorableplaces E / AndroidRuntime:位于android.app.ActivityThread.main(ActivityThread.java:5221)12-29 12:42:07.118 7519-7519 / inandroid.jeetna.memorableplaces E / AndroidRuntime:在java.lang.reflect.Method.invoke(本机方法)12-29 12:42:07.118 7519-7519 / inandroid.jeetna.memorableplaces E / AndroidRuntime:在java.lang.reflect.Method.invoke(Method.java: 372)12-29 12:42:07.118 7519-7519 / inandroid.jeetna.memorableplaces E / AndroidRuntime:在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899)12-29 12:42 :07.118 7519-7519 / inandroid.jeetna.memorableplaces E / AndroidRuntime:位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)12-29 12:42:07.118 7519-7519 / inandroid.jeetna。难忘的地方E / AndroidRuntime:由:java.lang引起。NullPointerException:尝试在空对象引用12-29 12:42上调用虚拟方法'void com.google.android.gms.maps.GoogleMap.setOnMapLongClickListener(com.google.android.gms.maps.GoogleMap $ OnMapLongClickListener)': 07.118 7519-7519 / inandroid.jeetna.memorableplaces E / AndroidRuntime:位于inandroid.jeetna.memorableplaces.MapsActivity.onCreate(MapsActivity.java:33)12-29 12:42:07.118 7519-7519 / inandroid.jeetna.memorableplaces E / AndroidRuntime:位于android.app.Activity.performCreate(Activity.java:5937)12-29 12:42:07.118 7519-7519 / inandroid.jeetna.memorableplaces E / AndroidRuntime:位于android.app.Instrumentation.callActivityOnCreate(Instrumentation.java :1105)12-29 12:42:07.118 7519-7519 / inandroid.jeetna.memorableplaces E / AndroidRuntime:位于android.app.ActivityThread。performLaunchActivity(ActivityThread.java:2251)12-29 12:42:07.118 7519-7519 / inandroid.jeetna.memorableplaces E / AndroidRuntime:at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)12-29 12:42 :07.118 7519-7519 / inandroid.jeetna.memorableplaces E / AndroidRuntime:at android.app.ActivityThread.access $ 800(ActivityThread.java:144)12-29 12:42:07.118 7519-7519 / inandroid.jeetna.memorableplaces E / AndroidRuntime:位于android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1278)12-29 12:42:07.118 7519-7519 / inandroid.jeetna.memorableplaces E / AndroidRuntime:位于android.os.Handler.dispatchMessage(Handler .java:102)12-29 12:42:07.118 7519-7519 / inandroid.jeetna.memorableplaces E / AndroidRuntime:at android.os.Looper.loop(Looper.java:135)12-29 12:42:07。118 7519-7519 / inandroid.jeetna.memorableplaces E / AndroidRuntime:位于android.app.ActivityThread.main(ActivityThread.java:5221)12-29 12:42:07.118 7519-7519 / inandroid.jeetna.memorableplaces E / AndroidRuntime:在java.lang.reflect.Method.invoke(本机方法)12-29 12:42:07.118 7519-7519 / inandroid.jeetna.memorableplaces E / AndroidRuntime:在java.lang.reflect.Method.invoke(Method.java: 372)12-29 12:42:07.118 7519-7519 / inandroid.jeetna.memorableplaces E / AndroidRuntime:在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899)12-29 12:42 :07.118 7519-7519 / inandroid.jeetna.memorableplaces E / AndroidRuntime:at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)java:5221)12-29 12:42:07.118 7519-7519 / inandroid.jeetna.memorableplaces E / AndroidRuntime:at java.lang.reflect.Method.invoke(Native Method)12-29 12:42:07.118 7519-7519 /inandroid.jeetna.memorableplaces E / AndroidRuntime:在java.lang.reflect.Method.invoke(Method.java:372)12-29 12:42:07.118 7519-7519 / inandroid.jeetna.memorableplaces E / AndroidRuntime:在com .android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899)12-29 12:42:07.118 7519-7519 / inandroid.jeetna.memorableplaces E / AndroidRuntime:at com.android.internal.os.ZygoteInit .main(ZygoteInit.java:694)java:5221)12-29 12:42:07.118 7519-7519 / inandroid.jeetna.memorableplaces E / AndroidRuntime:at java.lang.reflect.Method.invoke(Native Method)12-29 12:42:07.118 7519-7519 /inandroid.jeetna.memorableplaces E / AndroidRuntime:在java.lang.reflect.Method.invoke(Method.java:372)12-29 12:42:07.118 7519-7519 / inandroid.jeetna.memorableplaces E / AndroidRuntime:在com .android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899)12-29 12:42:07.118 7519-7519 / inandroid.jeetna.memorableplaces E / AndroidRuntime:at com.android.internal.os.ZygoteInit .main(ZygoteInit.java:694)难忘的地方E / AndroidRuntime:在java.lang.reflect.Method.invoke(Method.java:372)12-29 12:42:07.118 7519-7519 / inandroid.jeetna.memorableplaces E / AndroidRuntime:在com.android.internal。 os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899)12-29 12:42:07.118 7519-7519 / inandroid.jeetna.memorableplaces E / AndroidRuntime:at com.android.internal.os.ZygoteInit.main(ZygoteInit。 Java:694)难忘的地方E / AndroidRuntime:在java.lang.reflect.Method.invoke(Method.java:372)12-29 12:42:07.118 7519-7519 / inandroid.jeetna.memorableplaces E / AndroidRuntime:在com.android.internal。 os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899)12-29 12:42:07.118 7519-7519 / inandroid.jeetna.memorableplaces E / AndroidRuntime:at com.android.internal.os.ZygoteInit.main(ZygoteInit。 Java:694)

Pankaj.. 6

在您的Long click方法中,执行以下代码

public void onMapLongClick(LatLng point) {
       if(mMap != null){
        mMap.addMarker(new MarkerOptions()
                .position(point)
                .title("You are here")
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
      }
 }

并按以下方法将线mMap.setOnMapLongClickListener(this);从一个onCreate()方法移到onMapReady()另一个方法:

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        mMap.setOnMapLongClickListener(this);

        //your code
    }

这是因为在从方法mMap分配googleMap对象之前,您为nullonMapReady()



1> Pankaj..:

在您的Long click方法中,执行以下代码

public void onMapLongClick(LatLng point) {
       if(mMap != null){
        mMap.addMarker(new MarkerOptions()
                .position(point)
                .title("You are here")
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
      }
 }

并按以下方法将线mMap.setOnMapLongClickListener(this);从一个onCreate()方法移到onMapReady()另一个方法:

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        mMap.setOnMapLongClickListener(this);

        //your code
    }

这是因为在从方法mMap分配googleMap对象之前,您为nullonMapReady()

推荐阅读
mobiledu2402851377
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有