获取long和lat会在android map v2中返回空指针异常

时间:2014-05-19 03:30:03

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

我尝试按照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"/>

2 个答案:

答案 0 :(得分:0)

我不知道你在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));

答案 1 :(得分:0)

终于开始工作了。似乎问题是网络提供商。

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