位置计划崩溃

时间:2013-09-25 09:12:50

标签: android crash geolocation gps location

根据书中的教程制作应用程序,但有一些错误导致我的程序在未知的地方崩溃:logCat没有给出错误消息 所以我的位置类的代码:

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”后几秒钟 来自debuger的代码(如果有帮助)

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) 

1 个答案:

答案 0 :(得分:0)

这些是:

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

导致空指针

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