Android 处理程序未启动

Android handler does not start

我有一个应用程序,我从我使用 AsyncTask 的地址生成经度和纬度,我从 doInBackground 开始生成坐标 我的问题是在我的代码中似乎 Handler 是当我放这条线时没有打开:

viedotGeoCoo();

AsyncTask 之外似乎一切正常。

这是我的代码:

public class AddEvent extends Activity {
    Button addressButton, timeButton;
    TextView addressTV,textView2;
    TextView latLongTV, longCo, textView4;
    EditText editNosaukums;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_event);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        addressTV = (TextView) findViewById(R.id.addressTV);
        latLongTV = (TextView) findViewById(R.id.latLongTV);
        longCo = (TextView) findViewById(R.id.longCo);
        textView4 = (TextView) findViewById(R.id.textView4);
        editNosaukums = (EditText) findViewById(R.id.editNosaukums);
        textView2 = (TextView) findViewById(R.id.textView2);

        addressButton = (Button) findViewById(R.id.addressButton);
        addressButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {


                switch (view.getId()) {
                    case R.id.addressButton:
                        DownloadFilesTask task = new DownloadFilesTask();
                        task.execute((Void[]) null);
                        break;
                }

            }
        });
    }

    private class DownloadFilesTask extends AsyncTask<Void, Void, Void> {
        protected Void doInBackground(Void... urls) {
            Looper.prepare();
            viedotGeoCoo();

            return null;
        }

        protected void onProgressUpdate(Void... progress) {

        }

        protected void onPostExecute(Void result) {
             Log.e("Add_Event", "GEO IZDEVAAS");
             sutitDatus();

        }
    }



    public void viedotGeoCoo() {

        EditText editValsts = (EditText) findViewById(R.id.editValsts);
        EditText editPilseta = (EditText) findViewById(R.id.editPilseta);
        EditText editIelaNr = (EditText) findViewById(R.id.editIelaNr);

        String valsts = editValsts.getText().toString();
        String pilseta = editPilseta.getText().toString();
        String ielanr = editIelaNr.getText().toString();


        String address = valsts + " "+ pilseta + " " + ielanr;

        Log.e("ADD_EVENT", "HANDLER SAAKAS");


        GeocodingLocation locationAddress = new GeocodingLocation();
        locationAddress.getAddressFromLocation(address,
        getApplicationContext(), new GeocoderHandler());//jadublice jaataisa speciala klase


        GeocodingLocationLat locationAddressLat = new GeocodingLocationLat();
        locationAddressLat.getAddressFromLocation(address,
        getApplicationContext(), new GeocoderHandlerLat());//jadublice jaataisa speciala klase

        Log.e("ADD_EVENT", "GEO GENEREETS");

    }



    //sanjem stringu no com.wunderlist.slidinglayersample.GeocodingLocation.java
    private class GeocoderHandler extends Handler {
        @Override
        public void handleMessage(Message message) {

            Log.e("Add_Event", "HANDLER_LONG");

        }
    }

    private class GeocoderHandlerLat extends Handler {
        @Override
        public void handleMessage(Message message) {

            Log.e("Add_Event", "Handler_Lat");

        }

    }


}

有没有人知道为什么我的代码不能正常工作?

您在后台 Thread 中调用了 videotGeoConn() 但 Thread 在 doInBackground 之后退出 您可以从 super class 调用构造函数并传递主循环程序:

private class GeocoderHandlerLat extends Handler {
        public GeocoderHandlerLat(){
              super(Looper.getMainLooper());
        }
        @Override
        public void handleMessage(Message message) {

            Log.e("Add_Event", "Handler_Lat");

        }

    }

或者您可以将参数添加到 videotGeoConn 并在 onPreExecuteMethod 中创建处理程序:

private class DownloadFilesTask extends AsyncTask<Void, Void, Void> {
    private GeocoderHandlerLatHandler handler;
    protected void onPreExecute(){
            handler = new GeocoderHandlerLat();
    }    
    protected Void doInBackground(Void... urls) {
               // Looper.prepare() is not needed
               viedotGeoCoo(handler);

            return null;
        }
}