gpt4 book ai didi

android - 带有 OsmDroid 的 MapView 中的黑屏

转载 作者:行者123 更新时间:2023-11-29 21:04:53 26 4
gpt4 key购买 nike

我在 Android 中实现 OpenStreetMap MapView 时遇到问题。我正在使用 OsmDroid 库:问题是,当我在点击智能手机退出按钮(中间)后恢复应用程序时,或者当我点击最近的应用程序按钮时, map 没有显示,并且 View 停止在黑屏中。没有显示 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");
}

}

最佳答案

问题是您在 onPause() 中调用 setVisible(false),而不是在 onResume() 中调用 setVisibile(true)。考虑到在这种情况下您不需要使用 setVisible(bool) 方法;根据 Android 文档(链接):

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.

关于android - 带有 OsmDroid 的 MapView 中的黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24871666/

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