【发布时间】:2014-09-04 00:35:32
【问题描述】:
以下活动从我的解析数据库中获取数据并显示它。我有一个函数 getLocationFromAddress() 将字符串地址转换为 LatLng 格式。函数 prepareMap() 然后做一个标记,然后指向地图上的那个位置。活动代码(不显示不是imp的函数)如下
public class SingleRestraunt extends ActionBarActivity {
private GoogleMap map;
TextView resteName, resteCuisine, resteLocation, resteAddress, restePrice,
resteTimings, restePayment, resteRating, resteWebsite, next, prev;
String restName, obj, restCuisine, restLocation, restAddress, restPrice,
restTimings, restPayment, restRating, restWebsite;
String[] menu;
JSONArray restmenu;
WebView web;
int i = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single_restraunt);
resteName = (TextView) findViewById(R.id.restrauntName);
resteCuisine = (TextView) findViewById(R.id.restrauntCuisine);
resteLocation = (TextView) findViewById(R.id.restrauntLocation);
resteAddress = (TextView) findViewById(R.id.restrauntAddress);
restePrice = (TextView) findViewById(R.id.restrauntPrice);
resteTimings = (TextView) findViewById(R.id.restrauntTimings);
restePayment = (TextView) findViewById(R.id.restrauntPayment);
resteRating = (TextView) findViewById(R.id.restrauntRating);
resteWebsite = (TextView) findViewById(R.id.restrauntWebsite);
web = (WebView) findViewById(R.id.webView1);
next = (TextView) findViewById(R.id.next);
prev = (TextView) findViewById(R.id.prev);
Intent i = getIntent();
obj = i.getStringExtra("restId"); //gets the id of the restaurant whose
getDetails(obj); //details to be show
}
private void getDetails(String obj) {
ParseQuery<ParseObject> query = ParseQuery.getQuery("resdb");
query.getInBackground(obj, new GetCallback<ParseObject>() {
@Override
public void done(ParseObject object, ParseException e) {
if (e == null) {
restName = object.getString("name");
restCuisine = object.getString("cuisine");
restLocation = object.getString("locality");
restAddress = object.getString("address");
restPrice = object.getString("price");
restTimings = object.getString("timings");
restPayment = object.getString("accepted");
restRating = object.getString("ratings");
restWebsite = object.getString("URL");
restmenu = object.getJSONArray("menu");
addData();
// prepareMap();
} else {
e.printStackTrace();
}
}
});
}
public void addData() {
resteName.setText(restName);
resteCuisine.setText(restCuisine);
resteLocation.setText(restLocation);
resteAddress.setText(restAddress);
restePrice.setText(restPrice);
resteTimings.setText(restTimings);
restePayment.setText(restPayment);
resteRating.setText(restRating);
resteWebsite.setText(restWebsite);
if (restmenu != null) {
try {
String menu = (String) restmenu.get(i);
getMenu(menu);
} catch (JSONException e1) {
e1.printStackTrace();
}
}
}
public LatLng getLocationFromAddress(String strAddress) {
Geocoder coder = new Geocoder(this);
List<Address> address;
LatLng p1 = null;
try {
address = coder.getFromLocationName(strAddress, 5);
if (address != null && address.size() > 0) {
Address location = address.get(0);
p1 = new LatLng(location.getLatitude(), location.getLongitude());
} else {
p1 = new LatLng(19.111258, 72.908313);
}
} catch (IOException e) {
e.printStackTrace();
return p1;
}
return p1;
}
public void prepareMap() {
final LatLng REST = getLocationFromAddress(restAddress + ","
+ restLocation);
int zoomNiveau = 15;
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
map.addMarker(new MarkerOptions().position(REST).title(restName));//New error points here
map.moveCamera(CameraUpdateFactory.newLatLngZoom(REST, zoomNiveau));
}
最搞笑的是上面的activity并不是每次都崩溃。它似乎只对某些餐厅崩溃。错误日志显示如下
09-03 06:57:11.667: E/AndroidRuntime(2574): Process: com.example.gastronomaapp, PID: 2574
09-03 06:57:11.667: E/AndroidRuntime(2574): java.lang.NullPointerException
09-03 06:57:11.667: E/AndroidRuntime(2574): at com.example.gastronomaapp.SingleRestraunt.prepareMap(SingleRestraunt.java:204)
完全不知道是什么问题。我无法理解逻辑问题是什么,因为它似乎适用于某些餐厅。有什么想法吗?编辑在 cmets 中建议进行快速更改。但问题依然存在,只是错误发生了变化
【问题讨论】:
标签: android google-maps parse-platform google-maps-markers mapfragment