【发布时间】:2020-06-27 13:04:45
【问题描述】:
在sqlite中同样的方法可以,但是我把数据库换成Mysql,这个方法不行!
我从 AsyncTask 发送 arraylist(names) 以在 MapsActivity 中显示方法
和问题
myLocation = mmap.getMyLocation();
和
mMap.addMarker(new MarkerOptions().position(dz).title("欢迎来到")).showInfoWindow();
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
public void mark(Double latitude, Double longitude) {
LatLng dz = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(dz).title("welcome to")).showInfoWindow();
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(dz, 16));
}
public float distanceBetween(Double latitude, Double longitude) {
float result[] = new float[10];
Location myLocation = mMap.getMyLocation();
Double mylatitude = myLocation.getLatitude();
Double mylongitude = myLocation.getLongitude();
myLocation.distanceBetween(mylatitude, mylongitude, latitude, longitude, result);
return result[0];
}
public void show ( ArrayList names) {
ArrayList<String> arrList = new ArrayList<>();
arrList = names;
int c = arrList.size();
Float t[] = new Float[10];
Double company[] = new Double[3];
if (c > 0) {
int n = 0;
int i = 0;
while (c > 0) {
Double latitude = Double.valueOf(arrList.get(n));
Double longitude = Double.valueOf(arrList.get(n + 1));
Double price = Double.valueOf(arrList.get(n + 2));
t[i] = distanceBetween(latitude, longitude);
if (n == 0) {
company[0] = latitude;
company[1] = longitude;
company[2] = price;
}
if ((i > 0) && (t[i] < t[i - 1])) {
t[i] = t[i - 1];
company[0] = latitude;
company[1] = longitude;
company[2] = price;
}
i = i + 1;
n = n + 3;
c = c - 3;
}
mark(company[0], company[1]);
} else {
Toast T = Toast.makeText(this, "product is not available ", Toast.LENGTH_SHORT);
T.show();
}
Toast.makeText(this, "price of product = " + company[2], Toast.LENGTH_LONG).show();
}
}
public class Parser2 extends AsyncTask<Void,Void,Integer> {
Context c;
String data;
ArrayList<String> names=new ArrayList<>();
public Parser2(Context c, String data) {
this.c = c;
this.data = data;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Integer doInBackground(Void... params) {
return this.parse();
}
@Override
protected void onPostExecute(Integer integer) {
super.onPostExecute(integer);
if(integer==1)
{
MapsActivity m =new MapsActivity();
m.show(names);
}else {
Toast.makeText(c,"Unable to Parse2",Toast.LENGTH_SHORT).show();
}
}
private int parse()
{
try
{
JSONArray ja=new JSONArray(data);
JSONObject jo=null;
names.clear();
int i;
for( i=0;i<ja.length();i++)
{
jo=ja.getJSONObject(i);
String latitude=jo.getString("latitude");
String longitude=jo.getString("longitude");
String price=jo.getString("price");
names.add(latitude);
names.add(longitude);
names.add(price);}
return 1;
} catch (JSONException e) {
e.printStackTrace();
}
return 0;
}
}
【问题讨论】:
-
请包含您的应用停止的堆栈跟踪。
-
java.lang.NullPointerException:尝试在 com.example 的空对象引用上调用虚拟方法 'android.location.Location com.google.android.gms.maps.GoogleMap.getMyLocation()' .mapproject.MapsActivity.distanceBetween(MapsActivity.java:156) 在 com.example.mapproject.MapsActivity.show(MapsActivity.java:184) 在 com.example.mapproject.Parser2.onPostExecute(Parser2.java:43) 在 com。 example.mapproject.Parser2.onPostExecute(Parser2.java:13)
标签: java android api android-studio google-maps