Android处理程序无法启动

时间:2015-04-11 18:34:17

标签: android android-asynctask handler

我有应用程序,我从地址生成经度和纬度我使用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");

        }

    }


}

有谁知道为什么我的代码无法正常工作?

1 个答案:

答案 0 :(得分:0)

你在后台线程中调用videotGeoConn()但是在doInBackground之后线程正在退出 你可以从超类中调用一个构造函数并传递main looper:

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

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

        }

    }

或者你可以在videotGeoConn中添加一个paremeter并在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;
        }
}