解析lat和lng后,我的谷歌地图无法正常工作

时间:2013-08-14 07:45:15

标签: android

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

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

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

我的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);

map.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;
    }

}

logcat错误:

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

2 个答案:

答案 0 :(得分:2)

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

其次,你的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上买得起App。

答案 1 :(得分: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;
}