【问题标题】:Android handler does not startAndroid 处理程序未启动
【发布时间】:2015-04-11 18:34:16
【问题描述】:

我有一个应用程序,我从使用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");

        }

    }


}

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

【问题讨论】:

  • 只需将所有扩展视图并检索其文本值的代码移动到 UI 线程并将“地址”作为参数传递给 AsyncTask - 这将使您的代码更具可读性并解决您的问题.
  • @Egor no 那没有用

标签: android android-asynctask handler


【解决方案1】:

您在后台线程中调用 videotGeoConn() 但线程在 doInBackground 之后退出 您可以从超类调用构造函数并传递主循环器:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-08
    相关资源
    最近更新 更多