3

我正在开发应用程序,我可以在其中查看地图并在其上放置一些标记并缩放到该位置。

我成功地显示了我的地图视图。但是在添加标记和缩放代码后,它会强制关闭......请指导我,我在下面给出了我的代码以及我的日志猫帖子。

package com.appeal.my_map;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;



public class MainActivity extends FragmentActivity {
private GoogleMap googlemap;

double latitude = 17.385044;
double longitude = 78.486671;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    marker();

    CameraPosition cameraPosition = new CameraPosition.Builder().target(
            new LatLng(17.385044, 78.486671)).zoom(12).build();

googlemap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

}




private void marker() {
    // TODO Auto-generated method stub
    MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps ");
    googlemap.addMarker(marker);
    googlemap.setMyLocationEnabled(true);
}





@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}


}

我的xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<fragment 
      android:id="@+id/map"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      class="com.google.android.gms.maps.SupportMapFragment"/>

</LinearLayout>

原木猫

7561    com.appeal.my_map   AndroidRuntime  FATAL EXCEPTION: main
7561    com.appeal.my_map   AndroidRuntime  java.lang.RuntimeException: Unable     to start activity ComponentInfo{com.appeal.my_map/com.appeal.my_map.MainActivity}:     java.lang.NullPointerException
7561    com.appeal.my_map   AndroidRuntime  at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
7561    com.appeal.my_map   AndroidRuntime  at     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
7561    com.appeal.my_map   AndroidRuntime  at     android.app.ActivityThread.access$600(ActivityThread.java:162)
7561    com.appeal.my_map   AndroidRuntime  at     android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
7561    com.appeal.my_map   AndroidRuntime  at     android.os.Handler.dispatchMessage(Handler.java:107)
7561    com.appeal.my_map   AndroidRuntime  at     android.os.Looper.loop(Looper.java:194)
7561    com.appeal.my_map   AndroidRuntime  at     android.app.ActivityThread.main(ActivityThread.java:5371)
7561    com.appeal.my_map   AndroidRuntime  at     java.lang.reflect.Method.invokeNative(Native Method)
7561    com.appeal.my_map   AndroidRuntime  at     java.lang.reflect.Method.invoke(Method.java:525)
7561    com.appeal.my_map   AndroidRuntime  at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
7561    com.appeal.my_map   AndroidRuntime  at     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
7561    com.appeal.my_map   AndroidRuntime  at     dalvik.system.NativeStart.main(Native Method)
7561    com.appeal.my_map   AndroidRuntime  Caused by:     java.lang.NullPointerException
7561    com.appeal.my_map   AndroidRuntime  at     com.appeal.my_map.MainActivity.marker(MainActivity.java:41)
7561    com.appeal.my_map   AndroidRuntime  at     com.appeal.my_map.MainActivity.onCreate(MainActivity.java:25)
7561    com.appeal.my_map   AndroidRuntime  at     android.app.Activity.performCreate(Activity.java:5122)
7561    com.appeal.my_map   AndroidRuntime  at     android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
7561    com.appeal.my_map   AndroidRuntime  at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
7561    com.appeal.my_map   AndroidRuntime  ... 11 more

请纠正我哪里出错了...谢谢...!

4

2 回答 2

1

你还没有初始化googlemap。您需要初始化相同的

 SupportMapFragment fm = (SupportMapFragment)  getSupportFragmentManager().findFragmentById(R.id.map);
 googlemap = fm.getMap(); 
于 2013-10-07T13:04:07.130 回答
0

谷歌地图是空的?尝试先初始化 GoogleMap,然后添加标记。否则你会得到一个 NullPointer 异常。

googleMap = (GoogleMap)...findViewById(R.id.....);
于 2013-10-07T13:04:56.020 回答