简体   繁体   中英

How do I execute a class in another activity

I'm trying to execute a class(GetDirecoes, which is in the MapsActivity) from another activity (DetailsActivity), the idea is, when I click in the floating action button (DetailsActivity) I execute that class (GetDirecoes) and then close my current activity (DetailsActivity), when I close this activity it will automatically open my parent activity (MapsActivity) which is where the method GetDirecoes is. I'm trying to execute the method with this:

new MapsActivity.GetDirecoes().execute(latRepr, lngRepr);

But it doesn't work giving me the error:

MapsActivity' is not an enclosing class

The correction that android studio offers me is to make GetDirecoes static, but I can't do that because if I do it the entire class breaks, here's the GetDirecoes class:

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;
                }
            });
        }
    }

you can go through Call a Class From another class

private Activity activity;

you should initialize, activity

From another class, call it as :

((DetailsActivity) activity).refreshList(latRepr, lngRepr);

Now in, DetailsActivity.class you can do as,

public void refreshList(String latRepr, String lngRepr) {
      new GetDirecoes().execute(latRepr, lngRepr);
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM