【问题标题】:Getting long and lat returns a nullpointexception in android map v2在 android map v2 中获取 long 和 lat 返回一个空指针异常
【发布时间】:2014-05-19 03:30:03
【问题描述】:

我尝试按照 Google here 的教程进行操作。我相信我遵循了所写的每一条指令,但是当我尝试获取我的经纬度坐标时,我仍然得到 nullpointexception。

如果我按下地图中的目标图标将我重定向到我的当前位置,它会正常工作。但是当我以编程方式尝试时,它返回 nullpointexception。我尝试在 stackoverflow 中查找与我类似的情况,但到目前为止它们都不起作用。

如果您能提供我可以自己测试的示例工作源文件或代码,我们将不胜感激。以下是我的源代码,以防我遗漏了什么。提前致谢。

清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="sample.map.activity"
    android:versionCode="1"
    android:versionName="1.0" >

    <supports-screens
        android:largeScreens="true"
        android:normalScreens="true"
        android:smallScreens="false"
        android:xlargeScreens="true" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <uses-feature
        android:name="android.hardware.camera"
        android:required="false" />

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <uses-library android:name="com.google.android.maps" />

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <activity
            android:name="sample.map.activity"
            android:configChanges="orientation"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyAdqod8BHch40COYn19c6Ds0uhyfkX25SA" />
    </application>

</manifest>

地图活动类

import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

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

public class MapActivity extends ActionBarActivity {
    GoogleMap map;
    Location location;
    LatLng myLocation;
    LocationManager lm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        lm = (LocationManager) getSystemService(LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        String provider = lm.getBestProvider(criteria, true);

        map = ((MapFragment) getFragmentManager().findFragmentById(
                R.id.map)).getMap();

        map.setMyLocationEnabled(true);
        location = lm.getLastKnownLocation(provider);
        double mLat = location.getLatitude();
        double mLong = location.getLongitude();
        Toast.makeText(getApplicationContext(), mLat+" "+mLong, Toast.LENGTH_LONG).show();

        map.animateCamera(CameraUpdateFactory.newLatLngZoom(myLocation, 13));

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.map, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

这是堆栈跟踪

05-18 22:45:44.043: E/AndroidRuntime(1220): FATAL EXCEPTION: main
05-18 22:45:44.043: E/AndroidRuntime(1220): Process: sample.map.activity, PID: 1220
05-18 22:45:44.043: E/AndroidRuntime(1220): java.lang.RuntimeException: Unable to start activity ComponentInfo{sample.map.activity/sample.map.activity.MapActivity}: java.lang.NullPointerException
05-18 22:45:44.043: E/AndroidRuntime(1220):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-18 22:45:44.043: E/AndroidRuntime(1220):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-18 22:45:44.043: E/AndroidRuntime(1220):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-18 22:45:44.043: E/AndroidRuntime(1220):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-18 22:45:44.043: E/AndroidRuntime(1220):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-18 22:45:44.043: E/AndroidRuntime(1220):     at android.os.Looper.loop(Looper.java:136)
05-18 22:45:44.043: E/AndroidRuntime(1220):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-18 22:45:44.043: E/AndroidRuntime(1220):     at java.lang.reflect.Method.invokeNative(Native Method)
05-18 22:45:44.043: E/AndroidRuntime(1220):     at java.lang.reflect.Method.invoke(Method.java:515)
05-18 22:45:44.043: E/AndroidRuntime(1220):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-18 22:45:44.043: E/AndroidRuntime(1220):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-18 22:45:44.043: E/AndroidRuntime(1220):     at dalvik.system.NativeStart.main(Native Method)
05-18 22:45:44.043: E/AndroidRuntime(1220): Caused by: java.lang.NullPointerException
05-18 22:45:44.043: E/AndroidRuntime(1220):     at sample.map.activity.MapActivity.onCreate(MapActivity.java:39)
05-18 22:45:44.043: E/AndroidRuntime(1220):     at android.app.Activity.performCreate(Activity.java:5231)
05-18 22:45:44.043: E/AndroidRuntime(1220):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-18 22:45:44.043: E/AndroidRuntime(1220):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-18 23:04:59.383: W/ActivityThread(1354): ClassLoader.loadClass: The class loader returned by Thread.getContextClassLoader() may fail for processes that host multiple applications. You should explicitly specify a context class loader. For example: Thread.setContextClassLoader(getClass().getClassLoader());
05-18 23:04:59.813: I/Process(1354): Sending signal. PID: 1354 SIG: 9
05-18 23:05:01.123: D/dalvikvm(1401): GC_FOR_ALLOC freed 89K, 6% free 2960K/3128K, paused 26ms, total 28ms
05-18 23:05:01.123: I/dalvikvm-heap(1401): Grow heap (frag case) to 3.569MB for 635812-byte allocation
05-18 23:05:01.163: D/dalvikvm(1401): GC_FOR_ALLOC freed 2K, 5% free 3578K/3752K, paused 32ms, total 32ms
05-18 23:05:01.253: W/Visite Dates(1401): Reselected 0 tab name Unposted
05-18 23:05:01.463: D/(1401): HostConnection::get() New Host Connection established 0xb82b7f80, tid 1401
05-18 23:05:01.513: W/EGL_emulation(1401): eglSurfaceAttrib not implemented
05-18 23:05:01.523: D/OpenGLRenderer(1401): Enabling debug mode 0
05-18 23:51:25.343: W/EGL_emulation(1401): eglSurfaceAttrib not implemented
05-18 23:54:27.943: W/EGL_emulation(1401): eglSurfaceAttrib not implemented

这是我的布局的 xml 文件。

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.MapFragment"/>

【问题讨论】:

  • 请从 logcat 分享您的堆栈跟踪。
  • 我编辑了我的帖子。查看堆栈跟踪。
  • @philip MapActivity 中的第 39 行是什么?
  • 好的,这个堆栈跟踪表明 NPE 发生在您的主要活动的第 39 行。你在那条线上有什么?我想你的地图实例是空的,所以请分享你用于主要片段的布局。 (布局/activity_map.xml)。
  • 完成编辑。这是第 38 行的吐司 第 39 行是空的。这可能是导致零点的原因。 long 和 lat 必须返回 null。

标签: android google-maps android-maps-v2


【解决方案1】:

我不知道你在你的 xml 文件中做了什么,但是你的 xml 文件必须是这样的:

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

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

</RelativeLayout>

还添加以下内容,

< meta-data android:name="com.google.android.gms.version" 
         android:value="@integer/google_play_services_version" > 
< /meta-data>

在您的清单文件中,紧挨着第一个元数据标记(您放置 google api 密钥的位置)下方。

另外我还没有使用你编写的相机代码来动画相机。我的代码:

CameraPosition cameraPosition = new CameraPosition.Builder().target(
                new LatLng(your_lat, your_long)).zoom(12).build();

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

【讨论】:

  • 您可以查看我编辑的帖子以查看 xml 布局的样子。我的 xml 中已经有那行代码,并将其放在 api 键元数据下。它仍然显示相同的错误。您建议的方法不起作用,因为 lat 和 long 返回 null。我可以通过手动证明经纬度值将地图移动到我想要的任何坐标,但这不是我想要的。我想以编程方式获取用户的实际纬度。
  • @philip 检查位置是否为null 可能是
【解决方案2】:

终于搞定了。看来问题出在网络提供商上。

我没有使用 Criteria 和 getBestProvider,而是遵循 this 方法。它的作用是循环到网络提供商列表并手动返回您手机中现有的最佳提供商。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-03
    • 1970-01-01
    • 1970-01-01
    • 2014-08-25
    • 1970-01-01
    • 1970-01-01
    • 2013-07-27
    相关资源
    最近更新 更多