【问题标题】:Android: Googe Map is null although initializedAndroid:虽然已初始化,但谷歌地图为空
【发布时间】:2017-05-23 10:33:03
【问题描述】:

我有一个地图片段,里面有一个 MapView。 我在 onMapReady 方法中初始化了一个 Google 地图对象,并且我在开始时在此方法中设置的标记工作正常。但只要我想修改标记或在另一个自定义方法中设置一个新标记,我就会收到 NullPointerException 说我的 googleMap 对象为空。这不应该是因为它已经被初始化了。

非常感谢您的帮助。非常感谢您。

这是我的代码:

这是我的片段活动开始的样子:

public class MapsFragment extends Fragment implements OnMapReadyCallback {

MapView mMapView;
private GoogleMap googleMap;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_maps, container, false);

    mMapView = (MapView) rootView.findViewById(R.id.mapView);
    mMapView.onCreate(savedInstanceState);

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

    try {
        MapsInitializer.initialize(getActivity().getApplicationContext());
    } catch (Exception e) {
        e.printStackTrace();
    }

    mMapView.getMapAsync(this);

    return rootView;
}

这是 onMapReady 方法:

@Override
public void onMapReady(GoogleMap gMap) {
    // The global variable googleMap gets initialised
    googleMap = gMap;
    LatLng sydney = new LatLng(-33.852, 151.211);
    googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
    googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}

这是我自定义的用户调用方法:

 public void updateCamera(String search_string) {

   LatLng search_string_place = new LatLng(-37.9712371, 144.4927088);

   // THIS is where the NullPointer exception happens for the first time
   googleMap.addMarker(new 
   MarkerOptions().position(search_string_place).title(Melbourne));

   googleMap.moveCamera(CameraUpdateFactory.newLatLng(search_string_place)); 
 }

所有方法都在同一个类中:MapsFragment

编辑:

错误日志:

FATAL EXCEPTION: main
Process: <packagename>, PID: 26556
java.lang.IllegalStateException: Could not execute method of the activity
  at android.view.View$1.onClick(View.java:3830)
  at android.view.View.performClick(View.java:4445)
  at android.view.View$PerformClick.run(View.java:18446)
  at android.os.Handler.handleCallback(Handler.java:733)
  at android.os.Handler.dispatchMessage(Handler.java:95)
  at android.os.Looper.loop(Looper.java:136)
  at android.app.ActivityThread.main(ActivityThread.java:5146)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:515)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
  at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:515)
  at android.view.View$1.onClick(View.java:3825)
  ... 11 more
Caused by: java.lang.NullPointerException
  at <packagename>.MapsFragment.updateCamera(MapsFragment.java:134)
  at <packagename>.MainActivity.callUpdateCamera(MainActivity.java:46)
  ... 14 more

我的清单文件:

<?xml version="1.0" encoding="utf-8"?>

<!--
     The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
     Google Maps Android API v2, but you must specify either coarse or fine
     location permissions for the 'MyLocation' functionality. 
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    android:name="android.support.multidex.MultiDexApplication">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <!--
         The API key for Google Maps-based APIs is defined as a string resource.
         (See the file "res/values/google_maps_api.xml").
         Note that the API key is linked to the encryption key used to sign the APK.
         You need a different API key for each encryption key, including the release key that is used to
         sign the APK for publishing.
         You can define the keys for the debug and release targets in src/debug/ and src/release/. 
    -->
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />

</application>

编辑:

我通过将所有内容放在一个班级/活动中来解决了这个问题。然后所有的电话工作正常。我认为问题在于我创建了 MapsFragment 的一个新实例,而在这个新实例中,全局变量 googleMap 未定义。

非常感谢您的所有回答。

【问题讨论】:

  • 发布错误日志
  • 你是在onMapReady之前调用那个更新方法吗?如果是这样,是的,地图为空
  • 不,我不这么认为,因为该方法是通过按下按钮调用的,并且在初始标记可见后我按下按钮。
  • 发布你的清单文件
  • @Roy 你为什么要删除你的答案?

标签: java android google-maps nullpointerexception


【解决方案1】:

有同样的问题,当我将 GoogleMap 对象更改为静态时它工作了

【讨论】:

    【解决方案2】:

    我不知道您在哪里调用自定义方法,但您可以尝试将初始化的映射传递给您的方法,以确保您可以访问它。

    试试这个:

    public void updateCamera(String search_string, GoogleMap googleMap) {
    
       LatLng search_string_place = new LatLng(-37.9712371, 144.4927088);
    
       // THIS is where the NullPointer exception happens for the first time
       googleMap.addMarker(new 
       MarkerOptions().position(search_string_place).title(Melbourne));
    
       googleMap.moveCamera(CameraUpdateFactory.newLatLng(search_string_place)); 
     }
    

    【讨论】:

    • 这是个好主意,但不幸的是,这不是一个真正的选择,因为我从另一个活动中调用该方法,而我无法直接访问地图。只有我调用的方法可以访问。
    • 我通过创建 MapsFragment 类的实例来从我的主要活动中调用该方法,如下所示:public class MainActivity extends FragmentActivity { MapsFragment mf = new MapsFragment(); 就在 MainActivity 的开头,然后稍后按下一个按钮,我调用 @ 987654323@这会是错误的来源吗?
    • 我认为是这样,因为不要重新调整地图,也许你也需要在这里初始化地图。
    • 这是完整的代码,其余的主要活动包括一个标准的 onCreate 方法和一个用户定义的,由一个调用“callUpdateCamera”方法的按钮调用(见上文)。
    • 我只是不知道该怎么做,地图只能在 MapsFragment Activity 中初始化,如果我将全局变量 googleMap 公开并将其作为“mf.googleMap”传递给方法它仍然为空。
    猜你喜欢
    • 1970-01-01
    • 2016-03-11
    • 1970-01-01
    • 2012-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-24
    相关资源
    最近更新 更多