如何在另一个活动中执行课程
Posted
技术标签:
【中文标题】如何在另一个活动中执行课程【英文标题】:How do I execute a class in another activity 【发布时间】:2017-09-02 18:35:32 【问题描述】:我正在尝试从另一个活动 (DetailsActivity) 执行一个类(GetDirecoes,它在 MapsActivity 中),这个想法是,当我单击浮动操作按钮 (DetailsActivity) 时,我执行该类 (GetDirecoes) 并然后关闭我当前的活动(DetailsActivity),当我关闭此活动时,它将自动打开我的父活动(MapsActivity),这是 GetDirecoes 方法所在的位置。 我正在尝试使用以下方法执行该方法:
new MapsActivity.GetDirecoes().execute(latRepr, lngRepr);
但它给我错误不起作用:
MapsActivity' 不是封闭类
android studio 提供给我的更正是让 GetDirecoes 成为静态的,但我不能这样做,因为如果我这样做,整个类都会中断,这里是 GetDirecoes 类:
public class GetDirecoes extends AsyncTask<String, Void, Void> implements Serializable
@Override
protected void onPreExecute()
super.onPreExecute();
pDialog = new ProgressDialog(MapsActivity.this);
pDialog.setMessage("Aguarde...");
pDialog.setCancelable(false);
pDialog.show();
@Override
protected Void doInBackground(String... params)
HttpHandler sh = new HttpHandler();
txtDistanciaTotal = (TextView) findViewById(R.id.txtDistanciaTotal);
txtDuracaoTotal = (TextView) findViewById(R.id.txtDuracaoTotal);
instrucoes = (TextView) findViewById(R.id.Instrucoes);
String url1 = "https://maps.googleapis.com/maps/api/directions/json?origin=";
String url2 = "&destination=";
String url3 = "&key=AIzaSyBh6tmMrC6QaHMJewFGvGc8xOjc0WMajxQ&language=pt";
String latRepr = params[0];
String lngRepr = params[1];
final String jsonStr = sh.makeServiceCall(url1 + mylatitude + "," + mylongitude + url2 + latRepr + "," + lngRepr + url3);
Log.e(TAG, "Link: " + url1 + mylatitude + "," + mylongitude + url2 + latRepr + "," + lngRepr + url3);
Log.e(TAG, "Resposta do URL: " + jsonStr);
if (jsonStr != null)
try
JSONObject root = new JSONObject(jsonStr);
JSONArray routes = root.optJSONArray("routes");
if (routes != null)
JSONObject singleRout = (JSONObject) routes.get(0);
JSONArray legs = (JSONArray) singleRout.get("legs");
if (legs != null)
JSONObject singleLeg = (JSONObject) legs.get(0);
//Distancia total
JSONObject distanceT = (JSONObject) singleLeg.get("distance");
if (distanceT != null)
distT = distanceT.getString("text");
//Duracao Total
JSONObject durationT = (JSONObject) singleLeg.get("duration");
if (durationT != null)
duraT = durationT.getString("text");
JSONArray steps = (JSONArray) singleLeg.get("steps");
if (steps != null)
for (int j = 0; j < steps.length(); j++)
JSONObject singleStep = (JSONObject) steps.get(j);
HashMap<String, String> direcao = new HashMap<>();
JSONObject distance = (JSONObject) singleStep.get("distance");
//Distancia
if (distance != null)
String dist = distance.getString("text");
direcao.put("textDi", dist);
//Duraçao
JSONObject duration = (JSONObject) singleStep.get("duration");
if (duration != null)
String dura = duration.getString("text");
direcao.put("textDu", dura);
//Polylines
JSONObject singlePolyline = (JSONObject) singleStep.get("polyline");
if (singlePolyline != null)
String points = singlePolyline.getString("points");
Map<String, String> caminho = new HashMap<>();
caminho.put("points", points);
listaPolylines.add((HashMap<String, String>) caminho);
//Instruções
String html = singleStep.getString("html_instructions");
direcao.put("html_instructions2", html);
listaDirecoes.add(direcao);
catch (final JSONException e)
Log.e(TAG, "ERROR: " + e.getMessage());
runOnUiThread(new Runnable()
@Override
public void run()
Toast.makeText(getApplicationContext(), "ERROR: " + e.getMessage(), Toast.LENGTH_LONG).show();
);
else
Log.e(TAG, "ERROR");
runOnUiThread(new Runnable()
@Override
public void run()
Toast.makeText(getApplicationContext(), "Verifique a sua ligação à internet", Toast.LENGTH_LONG).show();
);
return null;
@Override
protected void onPostExecute(Void result)
super.onPostExecute(result);
final ListView list = (ListView) (MapsActivity.this).findViewById(R.id.listaDirecoes);
ListAdapter adapter = new SimpleAdapter(
(MapsActivity.this),
listaDirecoes,
R.layout.list_item_direcoes,
new String[]"html_instructions2", "textDi", "textDu",
new int[]R.id.Instrucoes, R.id.txtDistancia, R.id.txtDuraçao);
list.setAdapter(adapter);
txtDuracaoTotal.setText(duraT);
txtDistanciaTotal.setText(distT);
fazerCaminho(listaPolylines);
if (pDialog.isShowing())
pDialog.dismiss();
bottom_sheet.setOnTouchListener(new View.OnTouchListener()
public boolean onTouch(View v, MotionEvent event)
Log.v(TAG, "Parent Touch");
findViewById(R.id.listaDirecoes).getParent().requestDisallowInterceptTouchEvent(false);
return false;
);
listaDirecoesChild.setOnTouchListener(new View.OnTouchListener()
public boolean onTouch(View v, MotionEvent event)
Log.v(TAG, "CHILD TOUCH");
// Disallow the touch request for parent scroll on touch of child view
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
);
【问题讨论】:
【参考方案1】:您可以通过Call a Class From another class
private Activity activity;
你应该初始化,activity
从另一个类中,将其称为:
((DetailsActivity) activity).refreshList(latRepr, lngRepr);
现在,DetailsActivity.class 你可以这样做,
public void refreshList(String latRepr, String lngRepr)
new GetDirecoes().execute(latRepr, lngRepr);
【讨论】:
抱歉迟到了,不,它仍然说:“'MapsActivity' 不是封闭类”并要求我将 GetDirecoes 设为静态 我改变了应用程序的方向,现在我不再尝试使用它了,还是谢谢你以上是关于如何在另一个活动中执行课程的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 url 的 editText 和按钮单击在另一个活动中打开 web 视图