Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 我已经在MapActivity中实现了这个函数,但它使用的是网络,所以我需要在doInbackground中加入Asynctask_Android_Android Asynctask - Fatal编程技术网

Android 我已经在MapActivity中实现了这个函数,但它使用的是网络,所以我需要在doInbackground中加入Asynctask

Android 我已经在MapActivity中实现了这个函数,但它使用的是网络,所以我需要在doInbackground中加入Asynctask,android,android-asynctask,Android,Android Asynctask,我有Hesham Saeed写的代码。我想把方向函数放在AsyncTask中,因为它正在使用网络。我对安卓系统还不熟悉,该怎么做呢。 我需要在任务中移动方向 主要活动代码 package com.example.googlemaptst; import com.google.android.maps.GeoPoint; import com.google.android.maps.MapActivity; import com.google.android.maps.MapView; imp

我有Hesham Saeed写的代码。我想把方向函数放在AsyncTask中,因为它正在使用网络。我对安卓系统还不熟悉,该怎么做呢。 我需要在任务中移动方向

主要活动代码

package com.example.googlemaptst;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;

public class MapsActivity extends MapActivity
{    

/** Called when the activity is first created. */
Button b;
private ProgressDialog pDialog;
private  Route directions(final GeoPoint start, final GeoPoint dest)  {
   Parser parser;
    String jsonURL = "http://maps.google.com/maps/api/directions/json?";
    final StringBuffer sBuf = new StringBuffer(jsonURL);
    sBuf.append("origin=");
    sBuf.append(start.getLatitudeE6()/1E6);
    sBuf.append(',');
    sBuf.append(start.getLongitudeE6()/1E6);
    sBuf.append("&destination=");
    sBuf.append(dest.getLatitudeE6()/1E6);
    sBuf.append(',');
    sBuf.append(dest.getLongitudeE6()/1E6);
    sBuf.append("&sensor=true&mode=driving");
    Log.v("I came in URL", sBuf.toString());
    parser = new GoogleParser(sBuf.toString());
    Route r =  parser.parse();
    return r;
}

@Override
public void onCreate(Bundle savedInstanceState)
{
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_map);
     MapView mapView = (MapView) findViewById(R.id.mapView); //or you can declare it directly with the API key
     Route route = directions(new GeoPoint((int)(26.2*1E6),(int)(50.6*1E6)), new GeoPoint((int)(26.3*1E6),(int)(50.7*1E6)));
     RouteOverlay routeOverlay = new RouteOverlay(route, Color.BLUE);
     mapView.getOverlays().add(routeOverlay);

}

@Override
protected boolean isRouteDisplayed() {

    return false;
}
}

好的,下面是你必须做的:

    MapView mapView = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_map);
     mapView = (MapView) findViewById(R.id.mapView); //or you can declare it directly with the API key
     DirectionsTask getDirectionsTask = new DirectionsTask();
     getDirectionsTask.execute(new GeoPoint((int)(26.2*1E6),(int)(50.6*1E6)), new GeoPoint((int)(26.3*1E6),(int)(50.7*1E6)));

}

private class DirectionsTask extends AsyncTask<GeoPoint, Void, Route> {

    protected Route doInBackground(GeoPoint... geoPointParams) {
        GeoPoint start = geoPointParams[0];
        GeoPoint dest = geoPoint[1];

           Parser parser;
            String jsonURL = "http://maps.google.com/maps/api/directions/json?";
            final StringBuffer sBuf = new StringBuffer(jsonURL);
            sBuf.append("origin=");
            sBuf.append(start.getLatitudeE6()/1E6);
            sBuf.append(',');
            sBuf.append(start.getLongitudeE6()/1E6);
            sBuf.append("&destination=");
            sBuf.append(dest.getLatitudeE6()/1E6);
            sBuf.append(',');
            sBuf.append(dest.getLongitudeE6()/1E6);
            sBuf.append("&sensor=true&mode=driving");
            Log.v("I came in URL", sBuf.toString());
            parser = new GoogleParser(sBuf.toString());
            Route r =  parser.parse();
            return r;
    }


}

protected void onPostExecute(Route route) {
    RouteOverlay routeOverlay = new RouteOverlay(route, Color.BLUE);
     mapView.getOverlays().add(routeOverlay);
}
MapView MapView=null;
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
mapView=(mapView)findViewById(R.id.mapView);//或者您可以直接用API键声明它
DirectionTask GetDirectionTask=新建DirectionTask();
执行(新地质点((int)(26.2*1E6),(int)(50.6*1E6)),新地质点((int)(26.3*1E6),(int)(50.7*1E6));
}
私有类DirectionTask扩展了AsyncTask{
受保护的路由doInBackground(地质点…地质点参数){
地质点开始=地质点参数[0];
地质点dest=地质点[1];
解析器;
字符串jsonURL=”http://maps.google.com/maps/api/directions/json?";
最终StringBuffer sBuf=新StringBuffer(jsonURL);
sBuf.append(“origin=”);
sBuf.append(start.getLatitudeE6()/1E6);
sBuf.append(',');
sBuf.append(start.getLongitudeE6()/1E6);
sBuf.append(“&destination=”);
sBuf.append(dest.getLatitudeE6()/1E6);
sBuf.append(',');
sBuf.append(dest.getLongitudeE6()/1E6);
sBuf.append(“&sensor=true&mode=driving”);
Log.v(“我来自URL”,sBuf.toString());
parser=newGoogleParser(sBuf.toString());
路由r=parser.parse();
返回r;
}
}
受保护的void onPostExecute(路由){
RouteOverlay RouteOverlay=新的RouteOverlay(路线,颜色.蓝色);
mapView.getOverlays().add(routeOverlay);
}

几点意见。将mapView置于OnCreate之外,以便onPostExecute可以访问它。请注意,onPostExecute在UI线程中被调用,因此没有问题。

public void execute(List gp,int i,Route Route){//TODO Auto-generated method stub}在尝试使用新的ReadConfig(getApplicationContext())时显示此错误。execute(“”;}我甚至尝试使用这个Route=new ReadConfig().doInBackground(gp);但是我得到了错误android.os.NetworkOnMainThreadException plz帮助..谢谢。。。它很有魅力。。在经历了一个漫长的夜晚后的早晨。。。。
new ReadConfig(getApplicationContext()).execute("");
    MapView mapView = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_map);
     mapView = (MapView) findViewById(R.id.mapView); //or you can declare it directly with the API key
     DirectionsTask getDirectionsTask = new DirectionsTask();
     getDirectionsTask.execute(new GeoPoint((int)(26.2*1E6),(int)(50.6*1E6)), new GeoPoint((int)(26.3*1E6),(int)(50.7*1E6)));

}

private class DirectionsTask extends AsyncTask<GeoPoint, Void, Route> {

    protected Route doInBackground(GeoPoint... geoPointParams) {
        GeoPoint start = geoPointParams[0];
        GeoPoint dest = geoPoint[1];

           Parser parser;
            String jsonURL = "http://maps.google.com/maps/api/directions/json?";
            final StringBuffer sBuf = new StringBuffer(jsonURL);
            sBuf.append("origin=");
            sBuf.append(start.getLatitudeE6()/1E6);
            sBuf.append(',');
            sBuf.append(start.getLongitudeE6()/1E6);
            sBuf.append("&destination=");
            sBuf.append(dest.getLatitudeE6()/1E6);
            sBuf.append(',');
            sBuf.append(dest.getLongitudeE6()/1E6);
            sBuf.append("&sensor=true&mode=driving");
            Log.v("I came in URL", sBuf.toString());
            parser = new GoogleParser(sBuf.toString());
            Route r =  parser.parse();
            return r;
    }


}

protected void onPostExecute(Route route) {
    RouteOverlay routeOverlay = new RouteOverlay(route, Color.BLUE);
     mapView.getOverlays().add(routeOverlay);
}