2016-11-14 87 views
0

我試圖創建一個小應用程序,它可以找到用戶當前所處的x和y座標。問題是我剛得到一個空指針異常在行143Android Studios位置

double lon = loc.getLongitude(); 

我覺得我getLastKnownLocation()方法返回null。我通過虛擬設備運行它。下面的代碼。

package com.team23.profilebuilder; 

import android.Manifest; 
import android.content.Context; 
import android.content.pm.PackageManager; 
import android.location.Location; 
import android.location.LocationListener; 
import android.os.Bundle; 
import android.support.v4.app.ActivityCompat; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
import android.util.Log; 
import android.location.LocationManager; 

import java.util.List; 

public class FindLocationActivity extends CustomActivity 
{ 

// Buttons to be used 
private Button locButton, saveButton; 
// Textview to be displayed/used 
private TextView locText, latText, longText, latVal, longVal; 
// Location objects 
private LocationManager locMan; 
private LocationListener locLis; 
private Location loc; 

private static final String TAG = "FindLocationActivity"; 


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

    // Initialise all elements 
    initialiseUIElements(); 
    initialiseOtherElements(); 

    // Set button listeners 
    setListeners(); 
} 

private void initialiseUIElements() 
{ 
    locButton = (Button) findViewById(R.id.locButton); 
    saveButton = (Button) findViewById(R.id.saveButton); 
    locText = (TextView) findViewById(R.id.locText); 
    latText = (TextView) findViewById(R.id.latText); 
    longText = (TextView) findViewById(R.id.longText); 
    latVal = (TextView) findViewById(R.id.latVal); 
    longVal = (TextView) findViewById(R.id.longVal); 
} 

private void initialiseOtherElements() 
{ 
    // Acquire a reference to the system LocationManager 
    locMan = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 

    // Define a listener which responds to location updates 
    locLis = new LocationListener() 
    { 
     // Method called when a new location is found 
     @Override 
     public void onLocationChanged(Location location) 
     { 
      loc = location; 
     } 

     @Override 
     public void onStatusChanged(String s, int i, Bundle bundle) 
     { 

     } 

     @Override 
     public void onProviderEnabled(String s) 
     { 

     } 

     @Override 
     public void onProviderDisabled(String s) 
     { 

     } 
    }; 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) 
    { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     return; 
    } 
    // Register the listener with Location Manager to receive updates 
    locMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locLis); 
} 

private void setListeners() 
{ 
    locButton.setOnClickListener(new View.OnClickListener() 
    { 
     @Override 
     public void onClick(View view) 
     { 
      findLocation(); 
     } 
    }); 
    saveButton.setOnClickListener(new View.OnClickListener() 
    { 
     @Override 
     public void onClick(View view) 
     { 
      saveLocation(); 
     } 
    }); 
} 

private void findLocation() 
{ 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) 
    { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     return; 
    } 
    // Retrieve last known location 
    loc = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

    // Retrieve values 
    double lon = loc.getLongitude(); 
    double lat = loc.getLatitude(); 

    // Set TextView values 
    longVal.setText(Double.toString(lon)); 
    latVal.setText(Double.toString(lat)); 
} 

private void saveLocation() 
{ 
    Log.v(TAG, "SaveLocation Method"); 
} 
} 
+0

檢查空這樣的,如果(位置!= NULL){ 雙緯度= location.getLatitude(); double longitude = location.getLongitude(); } –

+0

問題是它是空的,我希望它實際上找到的位置,我已經知道它是空的,因爲這是什麼導致我的異常。 – user7085794

+0

按照這個教程http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/ –

回答

0

我測試了下面的源代碼,沒有運行時權限與目標22,它對我來說工作正常。

檢查位置,並檢查GPS在您的設備中已打開..

package com.team23.profilebuilder; 

import android.Manifest; 
import android.content.Context; 
import android.content.pm.PackageManager; 
import android.location.Location; 
import android.location.LocationListener; 
import android.os.Bundle; 
import android.support.v4.app.ActivityCompat; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
import android.util.Log; 
import android.location.LocationManager; 

import java.util.List; 

public class FindLocationActivity extends CustomActivity 
{ 

// Buttons to be used 
private Button locButton, saveButton; 
// Textview to be displayed/used 
private TextView locText, latText, longText, latVal, longVal; 
// Location objects 
private LocationManager locMan; 
private LocationListener locLis; 
private Location loc; 

private static final String TAG = "FindLocationActivity"; 


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

    // Initialise all elements 
    initialiseUIElements(); 
    initialiseOtherElements(); 

    // Set button listeners 
    setListeners(); 
} 

private void initialiseUIElements() 
{ 
    locButton = (Button) findViewById(R.id.locButton); 
    saveButton = (Button) findViewById(R.id.saveButton); 
    locText = (TextView) findViewById(R.id.locText); 
    latText = (TextView) findViewById(R.id.latText); 
    longText = (TextView) findViewById(R.id.longText); 
    latVal = (TextView) findViewById(R.id.latVal); 
    longVal = (TextView) findViewById(R.id.longVal); 
} 

private void initialiseOtherElements() 
{ 
    // Acquire a reference to the system LocationManager 
    locMan = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 

    // Define a listener which responds to location updates 
    locLis = new LocationListener() 
    { 
     // Method called when a new location is found 
     @Override 
     public void onLocationChanged(Location location) 
     { 
      loc = location; 
     } 

     @Override 
     public void onStatusChanged(String s, int i, Bundle bundle) 
     { 

     } 

     @Override 
     public void onProviderEnabled(String s) 
     { 

     } 

     @Override 
     public void onProviderDisabled(String s) 
     { 

     } 
    }; 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) 
    { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     return; 
    } 
    // Register the listener with Location Manager to receive updates 
    locMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locLis); 
} 

private void setListeners() 
{ 
    locButton.setOnClickListener(new View.OnClickListener() 
    { 
     @Override 
     public void onClick(View view) 
     { 
      findLocation(); 
     } 
    }); 
    saveButton.setOnClickListener(new View.OnClickListener() 
    { 
     @Override 
     public void onClick(View view) 
     { 
      saveLocation(); 
     } 
    }); 
} 

private void findLocation() 
{ 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) 
    { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     return; 
    } 
    // Retrieve last known location 
    loc = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

    if (loc!=null) { 
     // Retrieve values 
     double lon = loc.getLongitude(); 
     double lat = loc.getLatitude(); 
     // Set TextView values 
     longVal.setText(Double.toString(lon)); 
     latVal.setText(Double.toString(lat)); 
    } 
} 

private void saveLocation() 
{ 
    Log.v(TAG, "SaveLocation Method"); 
} 
}