0

我想知道我的谷歌地图有什么问题。当我尝试将一个类的纬度和经度的值解析为另一个类以使用谷歌地图时,它不起作用。

我从“”获取 URL,我需要“纬度”和“经度”的值来解析到我的谷歌地图。我能够检索纬度和经度的数据。

但是,当我尝试将值解析为谷歌地图时,它不起作用,给我的强制关闭错误。我可以知道我的代码有什么问题吗?

我的onclick部分代码:

public void onClick(View v) {
    // TODO Auto-generated method stub
        Log.d("coords", "lat: " + KEY_LATITUDE + " long: " + KEY_LONGITUDE);
        Intent intent = new Intent(SingleMenuItemActivity.this, map.class);
        intent.putExtra("LAT", KEY_LATITUDE.toString());
        intent.putExtra("LON", KEY_LONGITUDE.toString());
        startActivity(intent);

地图.java

public class map extends FragmentActivity {
    // double lat;
    // double lng;

    private GoogleMap map;

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

        SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        map = fm.getMap();
        map.animateCamera(CameraUpdateFactory.zoomIn());
        String lat = null;
        String lon = null;
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(LatLng(lat, lon),
                14.0f));

    }

    private LatLng LatLng(String lat, String lon) {
        // TODO Auto-generated method stub

        Bundle extras = getIntent().getExtras();
        lat = extras.getString("LAT");
        lon = extras.getString("LON");
        return null;
    }

}

日志错误:

08-14 15:21:47.300: E/AndroidRuntime(24718): FATAL EXCEPTION: main
08-14 15:21:47.300: E/AndroidRuntime(24718): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fambond/com.example.fambond.map}: java.lang.NullPointerException
08-14 15:21:47.300: E/AndroidRuntime(24718):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
08-14 15:21:47.300: E/AndroidRuntime(24718):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
08-14 15:21:47.300: E/AndroidRuntime(24718):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-14 15:21:47.300: E/AndroidRuntime(24718):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
08-14 15:21:47.300: E/AndroidRuntime(24718):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-14 15:21:47.300: E/AndroidRuntime(24718):    at android.os.Looper.loop(Looper.java:130)
08-14 15:21:47.300: E/AndroidRuntime(24718):    at android.app.ActivityThread.main(ActivityThread.java:3691)
08-14 15:21:47.300: E/AndroidRuntime(24718):    at java.lang.reflect.Method.invokeNative(Native Method)
08-14 15:21:47.300: E/AndroidRuntime(24718):    at java.lang.reflect.Method.invoke(Method.java:507)
08-14 15:21:47.300: E/AndroidRuntime(24718):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
08-14 15:21:47.300: E/AndroidRuntime(24718):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
08-14 15:21:47.300: E/AndroidRuntime(24718):    at dalvik.system.NativeStart.main(Native Method)
08-14 15:21:47.300: E/AndroidRuntime(24718): Caused by: java.lang.NullPointerException
08-14 15:21:47.300: E/AndroidRuntime(24718):    at maps.al.a.a(Unknown Source)
08-14 15:21:47.300: E/AndroidRuntime(24718):    at maps.ag.af.a(Unknown Source)
08-14 15:21:47.300: E/AndroidRuntime(24718):    at maps.ag.s.a(Unknown Source)
08-14 15:21:47.300: E/AndroidRuntime(24718):    at maps.ag.an.a(Unknown Source)
08-14 15:21:47.300: E/AndroidRuntime(24718):    at bkw.onTransact(SourceFile:83)
08-14 15:21:47.300: E/AndroidRuntime(24718):    at android.os.Binder.transact(Binder.java:279)
08-14 15:21:47.300: E/AndroidRuntime(24718):    at com.google.android.gms.maps.internal.IGoogleMapDelegate$a$a.moveCamera(Unknown Source)
08-14 15:21:47.300: E/AndroidRuntime(24718):    at com.google.android.gms.maps.GoogleMap.moveCamera(Unknown Source)
08-14 15:21:47.300: E/AndroidRuntime(24718):    at com.example.fambond.map.onCreate(map.java:28)
08-14 15:21:47.300: E/AndroidRuntime(24718):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
08-14 15:21:47.300: E/AndroidRuntime(24718):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
08-14 15:21:47.300: E/AndroidRuntime(24718):    ... 11 more
4

2 回答 2

2

首先,LatLng 方法有 2 个 double 作为参数!

其次,您的 LatLng 方法返回 null,因此很明显您将获得 NPE 并且您的应用程序将关闭。

尝试相应地更改您的代码:

public void onClick(View v) {
// TODO Auto-generated method stub
    Log.d("coords", "lat: " + KEY_LATITUDE + " long: " + KEY_LONGITUDE);
    Intent intent = new Intent(SingleMenuItemActivity.this, map.class);
    intent.putExtra("LAT", KEY_LATITUDE);
    intent.putExtra("LON", KEY_LONGITUDE);
    startActivity(intent);

然后。

public class map extends FragmentActivity {
 double lat;
 double lng;

private GoogleMap map;

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

    lat = getIntent().getDoubleExtra("LAT");
    lon = getIntent().getDoubleExtra("LON");

    SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    map = fm.getMap();
    map.animateCamera(CameraUpdateFactory.zoomIn());
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(getLatLng()),
            14.0f));

}

private LatLng getLatLng() {
    // TODO Auto-generated method stub
    return new LatLng(lat,lon);
}

}

同样从您的代码中,我假设您仍然需要处理 Java!尝试更多地练习它,否则您将无法在 Android 上进行应用程序开发。

于 2013-08-14T07:53:58.910 回答
0

您需要对代码行进行一些更改。在您的私有 LatLng LatLng(String lat, String lon) 方法中:

private LatLng LatLng(String lat, String lon) {
    // TODO Auto-generated method stub

    Bundle extras = getIntent().getExtras();
    lat = extras.getString("LAT");
    lon = extras.getString("LON");

    // Add these lines of changed code
    Double slatObject = Double.parseDouble(lat) / 1E6;
    Double slngObject = Double.parseDouble(lon) / 1E6;
    LatLng latlng_points = new LatLng(slatObject,slngObject);
    return latlng_points;
}
于 2013-08-14T08:05:09.127 回答