试图在textViews中显示经度和纬度

时间:2011-12-28 17:21:40

标签: android android-location

我试图在用户按下获取位置按钮后在单独的文本视图中显示经度和纬度。所有代码似乎都对我好,但我得到空指针异常。仅当用户按下按钮时才更改位置,我不想做任何事情。

package com.seanhannon.fyp;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


public class FYPActivity extends Activity {

public Button getLocation;
public TextView LongCoord;
public TextView LatCoord;
public double longitude;
public double latitude;
public LocationManager lm;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, new MyLocationListener());

    getLocation = (Button)getLocation.findViewById(R.id.GetLocation);
    LongCoord = (TextView)LongCoord.findViewById(R.id.longCoord);
    LatCoord = (TextView)LatCoord.findViewById(R.id.LatCoord);

    getLocation.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View arg0) 
        {

            showCurrentLocation();
        }



    });

}


protected void showCurrentLocation() 
{
    // TODO Auto-generated method stub
    Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    latitude = location.getLatitude();
    longitude = location.getLongitude();

    LongCoord.setText(Double.toString(longitude));
    LatCoord.setText(Double.toString(latitude));
}

}

class MyLocationListener implements LocationListener
{

    @Override
    public void onLocationChanged(Location location) 
    {
        // TODO Auto-generated method stub



    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

}

XML文件通常具有精细和粗略位置等权限。

这是Logcat输出

 FATAL EXCEPTION: main
 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.seanhannon.fyp/com.seanhannon.fyp.FYPActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
at com.seanhannon.fyp.FYPActivity.onCreate(FYPActivity.java:33)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
... 11 more

1 个答案:

答案 0 :(得分:2)

这是你的代码和平

getLocation = (Button)getLocation.findViewById(R.id.GetLocation);
LongCoord = (TextView)LongCoord.findViewById(R.id.longCoord);
LatCoord = (TextView)LatCoord.findViewById(R.id.LatCoord);

使用以下代码更改

getLocation = (Button)findViewById(R.id.GetLocation);
LongCoord = (TextView)findViewById(R.id.longCoord);
LatCoord = (TextView)findViewById(R.id.LatCoord);

它将删除空指针异常...