gpt4 book ai didi

java - 在 Android 应用程序中进行地理编码

转载 作者:行者123 更新时间:2023-11-29 04:42:35 24 4
gpt4 key购买 nike

我无法弄清楚为什么我每次运行该应用程序时都会收到空指针异常。这个 android 应用程序找到了这个地方并使用地理编码在 map 上标记了它。但是我没有成功这样做并且没有得到知道错误在哪里。请帮助!主要 Activity 代码如下:

package com.dutt.rishabh.locator;

import android.location.Address;
import android.location.Geocoder;
import android.os.AsyncTask;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import java.io.IOException;
import java.util.List;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
EditText x;
Button bfind;
String location;
MarkerOptions markerOptions;

@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);

bfind=(Button)findViewById(R.id.bfind);
View.OnClickListener findClickListener =new View.OnClickListener() {
@Override
public void onClick(View view) {
x=(EditText)findViewById(R.id.etsearch);
location=x.getText().toString();
if(location!=null || location.equals("")){
new GeocoderTask().execute(location);
}
}
};
bfind.setOnClickListener(findClickListener);



}
private class GeocoderTask extends AsyncTask<String, Void, List<Address>>{
@Override
protected List<Address> doInBackground(String... strings) {
Geocoder geocoder = new Geocoder(getBaseContext());
List<Address> addresses=null;
try {
addresses=geocoder.getFromLocationName(strings[0],3);
} catch (IOException e) {
e.printStackTrace();
}
return addresses;
}
@Override
protected void onPostExecute(List<Address> addresses){
if(addresses==null || addresses.size()==0){
Toast.makeText(getBaseContext(),"No location found",Toast.LENGTH_SHORT).show();
}
mMap.clear();
for(int i=0;i<addresses.size();i++){
Address address= addresses.get(i);
LatLng latLng=new LatLng(address.getLatitude(),address.getLongitude());
String addressText=String.format("%s,%s",address.getMaxAddressLineIndex()>0?address.getAddressLine(0):"",address.getCountryName());
markerOptions=new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title(addressText);

mMap.addMarker(markerOptions);
if(i==0){
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}
}
}
}



/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;

// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}

请帮助并请指出我的错误,以便我理解。以下是 logcat:

07-26 15:02:02.264 30388-30388/com.dutt.rishabh.locator E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.dutt.rishabh.locator, PID: 30388
java.lang.NullPointerException
at com.dutt.rishabh.locator.MapsActivity$GeocoderTask.onPostExecute(MapsActivity.java:75)
at com.dutt.rishabh.locator.MapsActivity$GeocoderTask.onPostExecute(MapsActivity.java:57)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5345)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
at dalvik.system.NativeStart.main(Native Method)

最佳答案

地址空检查

for(int i=0;i<addresses.size();i++){
}

或添加回车

if(addresses==null || addresses.size()==0){
Toast.makeText(getBaseContext(),"No location found",Toast.LENGTH_SHORT).show();
return;
}

关于java - 在 Android 应用程序中进行地理编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38586193/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com