Android GPS坐标应用程序

Android GPS坐标应用程序,android,nullpointerexception,gps,Android,Nullpointerexception,Gps,我正试图为android编写一个简单的GPS坐标应用程序,但由于nullpointerexception,它不断崩溃。我的应用程序的代码看起来像其他GPS坐标应用程序的代码,我正在努力寻找代码中的错误。当我使用以下代码时,nullpointerexception似乎出现在onLocationChanged方法中:longText.setText(“经度为:”+location.getLongitude() logcat的消息“onLocationChanged Entered”显示在logcat

我正试图为android编写一个简单的GPS坐标应用程序,但由于nullpointerexception,它不断崩溃。我的应用程序的代码看起来像其他GPS坐标应用程序的代码,我正在努力寻找代码中的错误。当我使用以下代码时,nullpointerexception似乎出现在onLocationChanged方法中:longText.setText(“经度为:”+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"/>

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

答案就在这里,这并不明显,因为我没有将main.xml文件中的所有代码粘贴到下面

我的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>
我应该根据文本视图的ID而不是android:name:

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

嗨,我删除了代码中的onLocationChanged()。。。但它仍然会使用相同的nullpointerexception崩溃。我已经没有主意了。。。有什么建议吗?03-22 16:26:50.720:E/AndroidRuntime(19980):致命异常:main 03-22 16:26:50.720:E/AndroidRuntime(19980):java.lang.NullPointerException 03-22 16:26:50.720:E/AndroidRuntime(19980):在com.example.gpscoord.MainActivity.onLocationChanged(MainActivity.java:75)Ha!我发现了它为什么不起作用。这是一个新手的错误,我将在下面解释。
longText = (TextView) findViewById(R.id.latitude);
latText = (TextView) findViewById(R.id.longitude);
longText = (TextView) findViewById(R.id.TextView2);
latText = (TextView) findViewById(R.id.TextView1);