6

UIとは異なるスレッド(Runnableを実装する)にAndroidのアプリがあります

私はいくつかのデータ (緯度と経度の形式の GPS データ) を受け取り、このデータから私は

ジオコーダーに渡して正しい住所を見つけたい.....その後、ジオコーダーから返された住所をデータベースに保存します。

これらのことを行う方法は次のとおりです。

public class Client implemets Runnable{


public void run()
{

Geocoder myLocation=new Geocoder(getApllicationContext,Locale.getDefault());



}

}

しかし、ここでエラーが発生します:

Geocoder myLocation=new Geocoder(getApplicationContext,Locale.getDefault());

Runnableは誰だかわからないgetApplicationContext……代わりに「これ」でやってみたけど同じ話……

Geocoder コンストラクターに渡す正しいコンテキストはどれですか????

Geocoder コンストラクターは次のようになります。

Geocoder myLocation =new Geocoder(context,locale);

私の活動では、これを行います:

public class Server2 extends Activity {


public void onCreate(Bundle icicle) {


ClientThread_special client = new ClientThread_special(db);//here is where I start thread


        new Thread(client).start();
}


}

public class ClientThread_special implements Runnable {



 public ClientThread_special(DBAdapter db){

     this.db=db;
    }


 public void run() 

{

Geocoder myLocation=new Geocoder(getApllicationContext,Locale.getDefault());


}


}

コンストラクターをどのように変更すればよいですか

public ClientThread_special(DBAdapter db){

     this.db=db;

    }

Runnable に Server2 のコンテキストを含めるには?

4

2 に答える 2

10

ClientThreadSpecialあなたのクラスは別のクラス(内部クラスではない)だと思いますか?もしそうなら、呼び出し元のアクティビティからコンテキストを渡してみませんか?

内部クラスであれば、さらに簡単です。ClientThreadSpecial-class が -class (Activity を拡張する) の内部クラスであると仮定すると、次のMyActivityようなものを使用できます。

Geocoder myLocation=new Geocoder(MyActivity.this,Locale.getDefault());
于 2011-05-29T11:54:44.260 に答える
4

Runnable を参照しているthisため使用できません。thisRunnable は MyActivity 内で定義されているため (アクティビティ/サービス名に置き換えてください)、 を参照できますMyActivity.this

于 2011-05-29T11:36:13.327 に答える