2016-02-28 29 views
0

即时通讯尝试获取设备的位置到我的Android应用程序。我试图使用android开发人员网站上概述的指南,但是每当我在手机上运行代码时,应用程序立即停止。我的代码是:Android应用程序停止与上次的位置检查

public class MainActivity extends AppCompatActivity implements ConnectionCallbacks, OnConnectionFailedListener{ 

    protected static final String TAG = "MainActivity"; 

    protected GoogleApiClient mGoogleApiClient; 

    protected Location mLastLocation; 
    protected Double lonValue; 
    protected Double latValue; 

    protected String mLatitudeLabel; 
    protected String mLongitudeLabel; 
    protected TextView mLatitudeText; 
    protected TextView mLongitudeText; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     TextView lonView = (TextView) findViewById(R.id.lon); 

     buildGoogleApiClient(); 

     //connect to parse backend 
     Parse.initialize(this, "xxxx", "xxxx"); 
     ParseInstallation.getCurrentInstallation().saveInBackground(); 
     Log.d("myInstallation Id", ParseInstallation.getCurrentInstallation().getInstallationId()); 

     //get location 
     lonValue = mLastLocation.getLatitude(); 
     latValue = mLastLocation.getLongitude(); 

     //text update 

    } 

    public void openLocation(View v){ 
     startActivity(new Intent(MainActivity.this, LocationActivity.class)); 
    } 

    @Override 
    public void onConnected(Bundle bundle) { 

     //check build version on phone 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
      //check that app has permission to access data 
      if (checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || checkSelfPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { 
       //get information from location service 
       mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
       Log.d(TAG,mLatitudeLabel); 
      } 
     } 

     mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 


     // Provides a simple way of getting a device's location and is well suited for 
     // applications that do not require a fine-grained location and that do not need location 
     // updates. Gets the best and most recent location currently available, which may be null 
     // in rare cases when a location is not available. 

     buildGoogleApiClient(); 

     Toast.makeText(MainActivity.this, "Should have network", Toast.LENGTH_SHORT).show(); 
    } 

    /** 
    * Builds a GoogleApiClient. Uses the addApi() method to request the LocationServices API. 
    */ 
    protected synchronized void buildGoogleApiClient() { 
     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(LocationServices.API) 
       .build(); 
    } 

    @Override 
    protected void onStart() { 
     super.onStart(); 
     mGoogleApiClient.connect(); 
    } 

    @Override 
    protected void onStop() { 
     super.onStop(); 
     if (mGoogleApiClient.isConnected()) { 
      mGoogleApiClient.disconnect(); 
     } 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult result) { 
     // Refer to the javadoc for ConnectionResult to see what error codes might be returned in 
     // onConnectionFailed. 
     Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + result.getErrorCode()); 
    } 


    @Override 
    public void onConnectionSuspended(int cause) { 
     // The connection to Google Play services was lost for some reason. We call connect() to 
     // attempt to re-establish the connection. 
     Log.i(TAG, "Connection suspended"); 
     mGoogleApiClient.connect(); 
    } 

} 

logcat的

02-28 18:45:08.848 4048-4048/com.worcestercomputing.ryanlambert.toiletstall E/AndroidRuntime:致命异常:主 过程: com.worcestercomputing.ryanlambert.toiletstall,PID:4048 java.lang.RuntimeException:无法启动活动 ComponentInfo {com.worcestercomputing.ryanlambert.toiletstall/com.worcestercomputing.ryanlambert.toiletstall.MainActivity}: 显示java.lang.NullPointerException:尝试在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)一个空对象 参考 上调用虚拟方法 '双android.location.Location.getLatitude()' 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 在android.app.ActivityThread.-wrap11(ActivityThread.java) 在 android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344) 在android.os.Handler.dispatchMessage(Handler.java:102) at a ndroid.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com。 android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 引起:java.lang.NullPointerException:尝试调用虚拟 方法'double android.location.Location.getLatitude()'null object reference at com.worcestercomputing.ryanlambert.toiletstall.MainActivity.onCreate(MainActivi ty.java:63) 在android.app.Activity.performCreate(Activity.java:6251) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 在 android.app.ActivityThread.performLaunchActivity( ActivityThread.java:2369) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 在android.app.ActivityThread.-wrap11(ActivityThread.java) 在 android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1344) at android.os.Handler.dispatchMe ssage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method。调用(Native Method) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit。Java的:616)

+0

请问您可以发布logcat .. – Lal

+0

您是否在模拟器上运行它?您必须分别传递纬度和经度以便在模拟器上工作。此外,在现实生活中,getLastLocation有时可能返回null(用户可能从他购买手机的那一天起禁用了位置),因此您应该实施空检查。 –

+0

即时通讯在物理设备上运行。我怎么能实现这个空检查? (对不起,只是刚刚学习绳索) –

回答

0

显然,问题是,你叫

//get location 
lonValue = mLastLocation.getLatitude(); 
latValue = mLastLocation.getLongitude(); 
Activity.onCreate()

。 但mLastLocation当时仍然是null

请注意,onCreate(Bundle)是Activity初始化活动的生命周期回调 - 它是在创建Activity的新实例时首先要调用的。