-2

根据书中的教程制作应用程序,但是有一些错误导致我的程序在未知的地方崩溃:logCat don't give error message So code of my location class`:

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.widget.TextView;

public class MainActivity extends Activity {

    private LocationManager myLocationManager;
    private LocationListener myLocationListener;
    private TextView myLatitude, myLongitude;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        myLatitude = (TextView)findViewById(R.id.textView1);
        myLongitude = (TextView)findViewById(R.id.textView2);



        myLocationManager = (LocationManager)getSystemService(
          Context.LOCATION_SERVICE);

        myLocationListener = new MyLocationListener();

        myLocationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER,
                0,
                0,
                myLocationListener);

        //Get the current location in start-up
        myLatitude.setText(String.valueOf(
            myLocationManager.getLastKnownLocation(
                LocationManager.GPS_PROVIDER).getLatitude()));

        myLongitude.setText(String.valueOf(
            myLocationManager.getLastKnownLocation(
               LocationManager.GPS_PROVIDER).getLongitude()));
    }

    private class MyLocationListener implements LocationListener{

        public void onLocationChanged(Location argLocation) {
        // TODO Auto-generated method stub
        myLatitude.setText(String.valueOf(
          argLocation.getLatitude()));
        myLongitude.setText(String.valueOf(
          argLocation.getLongitude()));
        }

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

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

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

我正在向 MAnifest 文件添加下一个权限:在我的 Galaxy TAb 上启动应用程序后,我崩溃了,但在工具栏飞溅图标“GPS”代码中的几秒钟后(如果有帮助)

DalvikVM[localhost:8624]    
    Thread [<1> main] (Suspended (exception RuntimeException))  
        <VM does not provide monitor information>   
        ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2663  
        ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2679   
        ActivityThread.access$2300(ActivityThread, ActivityThread$ActivityRecord, Intent) line: 125 
        ActivityThread$H.handleMessage(Message) line: 2033  
        ActivityThread$H(Handler).dispatchMessage(Message) line: 99 
        Looper.loop() line: 123 
        ActivityThread.main(String[]) line: 4627    
        Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
        Method.invoke(Object, Object...) line: 521  
        ZygoteInit$MethodAndArgsCaller.run() line: 868  
        ZygoteInit.main(String[]) line: 626 
        NativeStart.main(String[]) line: not available [native method]  
    Thread [<6> Binder Thread #2] (Running) 
    Thread [<5> Binder Thread #1] (Running) 
4

1 回答 1

0

就是这几行:

//Get the current location in start-up
myLatitude.setText(String.valueOf(
myLocationManager.getLastKnownLocation(
LocationManager.GPS_PROVIDER).getLatitude()));

导致空指针

在触发 onLocationChanged() 之前,最后一个已知位置将为空

于 2013-09-25T09:26:14.347 回答