0

呼び出しようとすると、「looper.prepare と呼ばれていないスレッド内でハンドラーを作成できません」というエラーが表示されます。

locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);

ただし、スレッド内で呼び出さない場合 ( run() ) は正常に動作します。どうすればよいですか?

編集: MyAlarmService.java

private Handler handler = new Handler(){

    @Override
    public void handleMessage(Message msg) {
        // TODO Auto-generated method stub
        super.handleMessage(msg);
        System.out.println("Handle MSGGGGGGGGGG");

    }

};

@Override
public void onStart(Intent intent, int startId) {
 // TODO Auto-generated method stub
 super.onStart(intent, startId);

 WorkerThreads WT1 = new WorkerThreads("Thread1");
 WorkerThreads WT2 = new WorkerThreads("Thread2");
    WT1.start();
    WT2.start();
}

class WorkerThreads extends Thread{

    public WorkerThreads(String string) {
        // TODO Auto-generated constructor stub
        super(string);
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        super.run();

        if(getName().equals("Thread1")){
            System.out.println("MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM");
            GetAlarmCoordinates gcc = new GetAlarmCoordinates();
            gcc.init(getApplicationContext(),handler);
            /*try {
                wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }*/

              System.out.println(MyAlarmService.currentLatLon[0] + "   " + MyAlarmService.currentLatLon[1] );

        }else if(getName().equals("Thread2")){

        }


    }


}

GetAlarmCoordinates.java

public void init(Context cont, Handler handler){

    mHandler = handler;
    locManager = (LocationManager) cont.getSystemService(Context.LOCATION_SERVICE);
    onClick();
}

 public void onClick() {
//  progress.setVisibility(View.VISIBLE);
    // exceptions will be thrown if provider is not permitted.
    System.out.println("GetCurrentLocation");
    try {
        gps_enabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    } catch (Exception ex) {
    }
    try {
        network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    } catch (Exception ex) {
    }

    System.out.println("gps_enabled : "+gps_enabled+ " network_enabled : "+network_enabled);


    if (gps_enabled) {
        locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
    }

    if (network_enabled) {
        locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locListener);
    }
}

     class MyLocationListener implements LocationListener {

    @Override
    public void onLocationChanged(Location location) {
        if (location != null) {
            // This needs to stop getting the location data and save the battery power.
            locManager.removeUpdates(locListener); 
            //locationManager.requestLocationUpdates(GPS_PROVIDER, intervall, distance, listener);


            latitude =  location.getLatitude();
            londitude = location.getLongitude();

            MyAlarmService.currentLatLon[0] = latitude;
            MyAlarmService.currentLatLon[1] = londitude;


            System.out.println("Alarm Coodinates lat : "+latitude+"  lon : "+londitude);
            mHandler.sendEmptyMessage(0);
        //notify();



        } 
    }
4

3 に答える 3

1

locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);UIスレッドで実行する必要があると 思います。

ソリューション:

新しいスレッドを作成せずに、UI スレッドで実行するだけです。

走っている場合

`locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);`

別のクラスから、アクティビティ インスタンスを介してコードを実行できます。

activity.runOnUiThread(new Runnable() {

@Override
public void run() {
// Your code
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
}
});

これが役立つことを願っています。

于 2012-08-20T11:37:02.730 に答える
1

メソッドを使用してActivity.runOnUiThread()この例外を回避できます。スニペットは次のとおりです。

activity.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                    locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
            }
        });

お役に立てれば。

于 2012-08-20T11:34:38.783 に答える
0

Contextで使用できますgetMainLooper。コンテキストをパラメーターおよびユーザーロッパーとしてクラスに送信するだけです

locationManager.requestLocationUpdates(getProviderName(), time, meter, locationUpdateListener,context.getMainLooper());
于 2017-08-10T12:00:21.000 に答える