Hello Im newebie to android programming。我在这里继续得到nullreference错误。我正在使用Google Map API并使用azure webservice。我也是stackoverflow的新手,这就是为什么我的问题一团糟,我的语法也是如此。
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, LocationListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private GoogleMap mMap;
private double myLatitude, myLongitude;
private String myUrl;
private static Context context;
private Button set;
private EditText skillCari;
private TextView latlng;
private List<People> finalPeople = new ArrayList<>();
private List<People> myPeople = new ArrayList<>();
private double currentLatitude, currentLongitude;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
public Activity currentActivity = this;
private double minLatitude, maxLatitude, minLongitude, maxLongitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
MapsActivity.context = getApplicationContext();
mGoogleApiClient = new GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this)
.addApi(LocationServices.API).build();
mLocationRequest = LocationRequest.create().setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(10 * 1000).setFastestInterval(1 * 1000);
latlng = (TextView) findViewById(R.id.Latlng);
RecyclerView myRecyle = (RecyclerView) findViewById(R.id.RecycleView);
myRecyle.setHasFixedSize(true);
myRecyle.setLayoutManager(new LinearLayoutManager(this));
final AdapterCard myCard = new AdapterCard();
myRecyle.setAdapter(myCard);
skillCari = (EditText) findViewById(R.id.skill_cari);
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.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;
}
final Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
set = (Button) findViewById(R.id.butSearch);
set.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myCard.clearData();
BengService service = ServiceFactory.createRetrofitService(BengService.class, BengService.BENG_SERVICE);
service.getSkilledUser(skillCari.getText().toString())
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<List<People>>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
Log.e("GithubDemo", e.getMessage());
Log.e("GithubDemo", e.toString());
}
@Override
public void onNext(List<People> peoples) {
finalPeople = SaringPeople(peoples, location.getLatitude(), location.getLongitude());
myCard.addData(finalPeople);
TambahMap(mMap, finalPeople);
}
});
}
});
}
public ArrayList<People> SaringPeople(List<People> people, double userLatitude, double userLongitude) {
myLatitude = userLatitude;
myLongitude = userLongitude;
myPeople = people;
ArrayList<People> peopleSample = new ArrayList<>();
double R = 6371;
double radius = 0.300;
double longitudeMin, longitudeMaks, latitudeMin, latitudeMaks;
longitudeMin = (userLongitude - Math.toDegrees(radius / R / Math.cos(Math.toRadians(userLatitude))));
longitudeMaks = (userLongitude + Math.toDegrees(radius / R / Math.cos(Math.toRadians(userLatitude))));
latitudeMaks = (userLatitude + Math.toDegrees(radius / R));
latitudeMin = (userLatitude - Math.toDegrees(radius / R));
for (People myPeople : people) {
if ((myPeople.getLatitude() <= latitudeMaks) && (myPeople.getLatitude() >= latitudeMin) && (myPeople.getLongitude() <= longitudeMaks) && (myPeople.getLongitude() >= longitudeMin)) {
peopleSample.add(myPeople);
}
else{}
}
return peopleSample;
}
public void TambahMap(GoogleMap googleMap, List<People> people) {
Bitmap urlImage;
for (People mypeople : people) {
urlImage = getBitmap(mypeople.getPhotoURL());
LatLng haha = new LatLng(mypeople.getLatitude(), mypeople.getLongitude());
googleMap.addMarker(new MarkerOptions().position(haha).title(mypeople.getFullName()).icon(BitmapDescriptorFactory.fromBitmap(urlImage)));
}
}
public Bitmap getBitmap(String imageUrl) {
try {
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream input = connection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(input);
return bitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
@Override
public void onLocationChanged(Location location) {
currentLatitude = location.getLatitude();
currentLongitude = location.getLongitude();
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
@Override
public void onConnected(@Nullable Bundle bundle) {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.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;
}
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (location == null){
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
} else {
currentLatitude = location.getLatitude();
currentLongitude = location.getLongitude();
}
}
@Override
public void onConnectionSuspended(int i) {
}
我正在尝试运行方法&#34; TambahMap &#34;和&#34; SaringPeople &#34;点击按钮&#34; 但搜索&#34; 请帮帮我..
我从日志中收到此错误
09-26 00:16:04.018 6337-6337/com.example.beng.bengtuak E/GithubDemo: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference
09-26 00:16:04.018 6337-6337/com.example.beng.bengtuak E/GithubDemo: java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference
09-26 00:16:16.386 6337-6337/com.example.beng.bengtuak E/GithubDemo: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference
09-26 00:16:16.386 6337-6337/com.example.beng.bengtuak E/GithubDemo: java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference
答案 0 :(得分:0)
您从googleclient获取的位置对象似乎为空
teardown
为googleclient添加回调
final Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
您应该检查该位置是否为空,如果是,请求位置更新。