Issue
I've a problem implementing an OpenStreetMap MapView in Android. I'm using OsmDroid Library: the problem is that when I resume the application after clicked to the smartphone quit button (the center) or when I click the recent applications button, the map is not showed, and the view stops in a black screen. No ANR is showed. No exception messages in logcat. No anomalies printed. Only a black screen.
What is the problem? I've tried to debug the app, but I've no found the anomaly. this is the code:
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");
}
}
Solution
The problem is that you are calling setVisible(false) in onPause(), and not calling setVisibile(true) in onResume(). Consider that you don't need to use setVisible(bool) method in this case; according to Android documentation (link):
public void setVisible (boolean visible) Added in API level 3 Control whether this activity's main window is visible.This is intended only for the special case of an activity that is not going to show a UI itself, but can't just finish prior to onResume() because it needs to wait for a service binding or such. Setting this to false allows you to prevent your UI from being shown during that time. The default value for this is taken from the windowNoDisplay attribute of the activity's theme.
Answered By - user1432059
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.