我在Android中实现OpenStreetMap MapView时遇到了问题。 我正在使用OsmDroid库: 问题是当我点击智能手机退出按钮(中心)后恢复应用程序或单击最近的应用程序按钮时,未显示地图,并且视图在黑屏中停止。没有显示ANR。在logcat中没有异常消息。没有打印异常。只有一个黑屏。
有什么问题? 我试图调试应用程序,但我没有发现异常。 这是代码:
package it.presenteapp.ui;
import it.presenteapp.R;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import org.osmdroid.DefaultResourceProxyImpl;
import org.osmdroid.api.IGeoPoint;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.ItemizedIconOverlay;
import org.osmdroid.views.overlay.MinimapOverlay;
import org.osmdroid.views.overlay.OverlayItem;
import org.osmdroid.views.overlay.PathOverlay;
import org.osmdroid.views.overlay.ScaleBarOverlay;
import org.osmdroid.views.overlay.compass.CompassOverlay;
import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import it.presenteapp.network.NetworkResource;
import it.presenteapp.network.classForJSON.RouteResponse;
import it.presenteapp.utils.Config;
public class MapActivity extends Activity
{
private MapView mOsmv;
private Timer timerTask;
private boolean networkProviderEnabled;
private RouteResponse Route;
private GeoPoint currentLocation;
private Boolean doBackground =false;
private MyLocationNewOverlay partenza;
private GeoPoint arrivo;
private PathOverlay pathOverlay;
private Paint paint;
private MinimapOverlay miniMapOverlay;
private ScaleBarOverlay scaleBar;
private CompassOverlay compassOverlay;
private String nomeCognome;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
this.mOsmv = (MapView)findViewById(R.id.mapview);
Intent i=getIntent();
this.nomeCognome = i.getStringExtra("NomeCognome");
this.arrivo= new GeoPoint(i.getDoubleExtra("Latitudine",43.6156683),i.getDoubleExtra("Longitudine",13.5177356));
getActionBar().setTitle("Ricerca loculo: "+ nomeCognome);
}
@Override
protected void onResume()
{
super.onResume();
Log.e("MAPACTIVITY", "RESUME CALLED");
this.mOsmv.setTileSource(TileSourceFactory.CYCLEMAP);
this.mOsmv.setUseDataConnection(true);
this.mOsmv.setBuiltInZoomControls(true);
this.mOsmv.setMultiTouchControls(true);
this.mOsmv.setMinZoomLevel(4);
this.mOsmv.getController().setZoom(this.mOsmv.getMaxZoomLevel());
Log.e("1", "ok");
/* setto il marker di arrivo */
Drawable markerArrivo = this.getResources().getDrawable(R.drawable.arrivo);
ArrayList listItem = new ArrayList<OverlayItem>();
final OverlayItem itemArrivo = new OverlayItem(this.nomeCognome, "nato il "+ "\n"+ "morto il ", arrivo);
itemArrivo.setMarker(markerArrivo);
listItem.add(itemArrivo);
this.mOsmv.getOverlayManager().clear();
this.mOsmv.getOverlayManager().add(new ItemizedIconOverlay<OverlayItem>(listItem, new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
@Override
public boolean onItemSingleTapUp(int index, OverlayItem item) {
Toast.makeText(getApplicationContext(),itemArrivo.getTitle()+"\n"+itemArrivo.getSnippet(),Toast.LENGTH_SHORT).show();
return false;
}
@Override
public boolean onItemLongPress(int index, OverlayItem item) {
//Toast.makeText(getApplicationContext(),"LUUUNGO",Toast.LENGTH_SHORT).show();
return false;
}
}, new DefaultResourceProxyImpl(getApplicationContext())));
Log.e("2", "ok");
/* setto il marker di partenza/attuale posizione dell'utente */
partenza = new MyLocationNewOverlay(getApplicationContext(),this.mOsmv);
partenza.enableFollowLocation();
partenza.enableMyLocation();
this.mOsmv.getOverlayManager().add(partenza);
Log.e("3", "ok");
/* aggiungo la scala della mappa */
scaleBar = new ScaleBarOverlay(this.getBaseContext(), this.mOsmv.getResourceProxy());
scaleBar.setCentred(true);
this.mOsmv.getOverlayManager().add(scaleBar);
Log.e("4", "ok");
/* aggiungo la bussola della mappa */
compassOverlay = new CompassOverlay(this, this.mOsmv);
compassOverlay.enableCompass();
this.mOsmv.getOverlayManager().add(compassOverlay);
Log.e("5", "ok");
/* aggiungo la mini mappa */
miniMapOverlay = new MinimapOverlay(this, this.mOsmv.getTileRequestCompleteHandler());
this.mOsmv.getOverlayManager().add(miniMapOverlay);
Log.e("6", "ok");
/* setto le proprietà del disegno della rotta */
this.paint = new Paint();
paint.setColor(Color.BLUE);
paint.setAlpha(100);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(8);
Log.e("7", "ok");
/* inizializzo il routing via Graphopper */
this.startGraphopperTask();
Log.e("9", "ok");
}
@Override
protected void onPause() {
super.onPause();
Log.e("MAPACTIVITY","PAUSE CALLED");
this.timerTask.cancel();
this.mOsmv.setBuiltInZoomControls(false);
setVisible(false);
this.partenza.disableMyLocation();
this.partenza.disableFollowLocation();
this.mOsmv.getTileProvider().detach();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.map, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId())
{
case R.id.action_shortcut_map_credits:
Toast.makeText(this,"@ Univpm", Toast.LENGTH_SHORT).show();
return true;
case android.R.id.home:
onBackPressed();
return true;
default:
Log.e("premuto","altro");
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onDestroy()
{
Log.e("MAPACTIVITY","DESTROY CALLED");
super.onDestroy();
}
public void drawRoute(RouteResponse _Route)
{
if(this.pathOverlay != null)
this.mOsmv.getOverlayManager().remove(this.pathOverlay);
Log.e("new position","setted!");
PathOverlay newPathOverlay = new PathOverlay(Color.RED,MapActivity.this);
newPathOverlay.addPoints(_Route.getPaths()[0].getPoints().getCoordinatesGeoPoint());
newPathOverlay.setPaint(this.paint);
this.mOsmv.getOverlays().add(newPathOverlay);
this.pathOverlay = newPathOverlay;
TextView istruzioni= (TextView) findViewById(R.id.popup_ins);
istruzioni.setText(_Route.getPaths()[0].getInstructions()[0].getText());
istruzioni.setVisibility(TextView.VISIBLE);
doBackground=false;
}
public void centerPosition(View v)
{
this.mOsmv.getController().animateTo((IGeoPoint) partenza.getMyLocation());
while (true)
{
if (!this.mOsmv.isAnimating())
{
this.mOsmv.getController().setZoom(this.mOsmv.getMaxZoomLevel());
return;
}
}
}
private class GraphopperTask extends AsyncTask<Void, Void, Void>
{
RouteResponse puntiRotta;
@Override
protected void onPreExecute()
{
Log.e("task ricerc route", "partito!");
super.onPreExecute();
// before making http calls
}
@Override
protected Void doInBackground(Void... arg0)
{
NetworkResource net =new NetworkResource();
if(partenza.getMyLocation() != null)
{
try {
Log.e("FASE PRE-CHIAMATA ROUTE", "CORRETTA");
String StartPoint = new StringBuilder().append(partenza.getMyLocation().getLatitudeE6() / 1E6).append(",")
.append(partenza.getMyLocation().getLongitudeE6() / 1E6).toString();
String EndPoint = new StringBuilder().append(arrivo.getLatitudeE6() / 1E6).append(",")
.append(arrivo.getLongitudeE6() / 1E6).toString();
puntiRotta = net.getRoute(StartPoint, EndPoint);
Log.e("StartPoint", StartPoint);
Log.e("EndPoint", EndPoint);
Log.e("FASE PARSING ROUTE", "CORRETTA");
} catch (Exception e) {
Log.e("ERRORE", e.toString());
}
}
return null;
}
@Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
// After completing http call
// will close this activity and launch main activity
Route=puntiRotta;
drawRoute(Route);
}
}
private void startGraphopperTask()
{
final Handler handler = new Handler();
this.timerTask = new Timer();
TimerTask doAsynchronousTask = new TimerTask()
{
@Override
public void run()
{
handler.post(new Runnable()
{
public void run()
{
try
{
if(partenza.getMyLocation() != null)
{
GraphopperTask graphopperTask = new GraphopperTask();
// PerformBackgroundTask this class is the class that extends AsynchTask
graphopperTask.execute();
}
} catch (Exception e)
{
}
}
});
}
};
this.timerTask.schedule(doAsynchronousTask, Config.startRouteDelay, Config.updateRouteInterval);
Log.e("8", "ok");
}
}
答案 0 :(得分:2)
问题是你在onPause()中调用setVisible(false),而在onResume()中没有调用setVisibile(true)。 考虑到在这种情况下你不需要使用setVisible(bool)方法;根据Android文档(链接):
public void setVisible(boolean visible)在API级别3 Control中添加 此活动的主窗口是否可见。这仅供参考 对于不会显示UI的活动的特殊情况 本身,但不能只是在onResume()之前完成,因为它需要 等待服务绑定等。将此设置为false允许您 防止在此期间显示您的UI。默认 这个值来自于windowNoDisplay属性 活动的主题。