0

私はアンドロイドアプリケーションに取り組んでいます。私の活動では、次のコードを使用しています。

LocationResult locationResult = new LocationResult(){

        @Override
        public void gotLocation(Location location){
            //Got the location!




            Drawable marker = getResources().getDrawable(
                    R.drawable.currentlocationmarker);//android.R.drawable.btn_star_big_on
            int markerWidth = marker.getIntrinsicWidth();
            int markerHeight = marker.getIntrinsicHeight();
            marker.setBounds(0, markerHeight, markerWidth, 0);
            MyItemizedOverlay myItemizedOverlay = new MyItemizedOverlay(marker);
            currentmarkerPoint = new GeoPoint((int) (location.getLatitude() * 1E6),
                    (int) (location.getLongitude() * 1E6));

            currLocation = location;

            mBlippcoordinate = currentmarkerPoint;
            mBlippLocation = location;
            myItemizedOverlay.addItem(currentmarkerPoint, "", "");

            mBlippmapview.getOverlays().add(myItemizedOverlay);
            animateToCurrentLocation(currentmarkerPoint);


        }
    };


    MyLocation myLocation = new MyLocation();
    myLocation.getLocation(this, locationResult);

上記のコードを使用して、GPS またはネットワークから位置を検索しています。animateToCurrentLocation(currentmarkerPoint);メソッドには asynctask が含まれています。

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

前もって感謝します。

4

1 に答える 1

3

Looper がアタッチされていないスレッドから AsyncTask を作成して実行しようとすると、このエラーが発生します。AsyncTasks には、AsyncTask を開始したスレッドに「タスク完了」メッセージをポストするルーパーが必要です。

さて、あなたの本当の質問: Looper でスレッドを取得する方法は? すでに 1 つあることがわかりました: メインスレッドです。ドキュメントにも記載されているように、メインスレッドから AsyncTask を作成して .execute() する必要があります。doInBackground() はワーカー スレッドで (AsyncTask スレッドプールから) 実行され、そこでネットワークにアクセスできます。onPostExecute() は、メイン スレッドのハンドラー/ルーパー経由でポストされた後、メイン スレッドで実行されます。

于 2012-12-05T11:18:02.103 に答える