GPS协调应用程序

时间:2014-03-22 12:42:04

标签: android nullpointerexception gps

我正在尝试为Android编写一个简单的GPS坐标应用程序,但由于nullpointerexception它一直在崩溃。我的应用程序的代码看起来像其他GPS坐标应用程序的代码,我很难在我的代码中找到错误。当我使用以下代码时,nullpointerexception似乎出现在onLocationChanged方法中:longText.setText(“Longitude is:”+ location.getLongitude())。

logcat“onLocationChanged Entered”的消息显示在logcat中,因此有一个位置,换句话说,(location!= null)= true。

请参阅下面的完整代码:

public class MainActivity extends Activity implements LocationListener {

    public LocationManager locationMgr;
    private static final int MIN_TIME = 5000;
    private static final int MIN_DISTANCE = 10;
    public Location currentLocation;
    TextView longText;
    TextView latText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        longText = (TextView) findViewById(R.id.latitude);
        latText = (TextView) findViewById(R.id.longitude);
        locationMgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, MIN_DISTANCE, this);
    }

     @Override
        public void onResume() {
            super.onResume();

            try {
                /* defaulting to a place */
                Location hardFix = new Location("ATL");
                hardFix.setLatitude(39.931261);
                hardFix.setLongitude(-75.051267);
                hardFix.setAltitude(1);

                try {

                    Location gps = locationMgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    Location network = locationMgr.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (gps != null) currentLocation = (gps);
                    else if (network != null) currentLocation = (network);
                    else currentLocation = (hardFix);
                } catch (Exception ex2) {
                    currentLocation = (hardFix);
                }
                onLocationChanged(currentLocation);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } 

@Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        if (location != null) {
            Log.i("Location", "OnLocationChanged Entered");
        longText.setText("Longitude is: " + location.getLongitude());
        latText.setText("Latitude is: " + location.getLatitude());
        }
    }

   public void onPause() {
        super.onPause();
        locationMgr.removeUpdates(this);
    }

我的权限在清单中:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>

我的logcat:

03-22 14:35:49.695: I/Location(13099): OnLocationChanged Entered
03-22 14:35:49.695: D/AndroidRuntime(13099): Shutting down VM
03-22 14:35:49.695: W/dalvikvm(13099): threadid=1: thread exiting with uncaught exception (group=0x41df4700)
03-22 14:35:49.695: E/AndroidRuntime(13099): FATAL EXCEPTION: main
03-22 14:35:49.695: E/AndroidRuntime(13099): java.lang.NullPointerException
03-22 14:35:49.695: E/AndroidRuntime(13099):    at com.example.gpscoord.MainActivity.onLocationChanged(MainActivity.java:75)
03-22 14:35:49.695: E/AndroidRuntime(13099):    at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:255)
03-22 14:35:49.695: E/AndroidRuntime(13099):    at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:184)
03-22 14:35:49.695: E/AndroidRuntime(13099):    at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:200)
03-22 14:35:49.695: E/AndroidRuntime(13099):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-22 14:35:49.695: E/AndroidRuntime(13099):    at android.os.Looper.loop(Looper.java:137)
03-22 14:35:49.695: E/AndroidRuntime(13099):    at android.app.ActivityThread.main(ActivityThread.java:5419)
03-22 14:35:49.695: E/AndroidRuntime(13099):    at java.lang.reflect.Method.invokeNative(Native Method)
03-22 14:35:49.695: E/AndroidRuntime(13099):    at java.lang.reflect.Method.invoke(Method.java:525)
03-22 14:35:49.695: E/AndroidRuntime(13099):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
03-22 14:35:49.695: E/AndroidRuntime(13099):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
03-22 14:35:49.695: E/AndroidRuntime(13099):    at dalvik.system.NativeStart.main(Native Method)
03-22 14:35:51.430: I/Process(13099): Sending signal. PID: 13099 SIG: 9

2 个答案:

答案 0 :(得分:0)

这是因为您在代码中调用了onLocationChanged()。不要这样做 - 这是一个回调。这意味着它由系统事件驱动,即获得位置修复。

答案 1 :(得分:0)

所以这就是答案,因为我没有将我的main.xml文件中的所有代码都粘贴在SO上,所以不会显而易见。

我的main.xml文件如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:name="@+id/latitude"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="23sp"
         />

    <TextView
        android:id="@+id/textView2"
        android:name="@+id/longitude"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="14dp"
        android:textSize="23sp"
         />

</RelativeLayout>

如果查看代码,可以看到我将textviews引用为:

longText = (TextView) findViewById(R.id.latitude);
latText = (TextView) findViewById(R.id.longitude);

我应该根据他们的ID引用textviews,而不是他们的android:name:

longText = (TextView) findViewById(R.id.TextView2);
latText = (TextView) findViewById(R.id.TextView1);