1

我有一个MapActivity,首先我试图获取用户的当前位置。为了获取当前位置,我使用了AsyncTask概念。现在我想要实现的是向用户显示ProgressDialog直到获得当前位置。

这是布局代码:-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF"
    android:orientation="vertical" >

    <com.google.android.maps.MapView
        android:id="@+id/mapview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:apiKey="0nO35c3MpXJisxsknk_w_fqVQa78sPhMQK3Yf5w"
        android:clickable="true" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <SeekBar
            android:id="@+id/zoombar"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:max="20"
            android:progress="0" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="70dip"
        android:layout_marginTop="5dip"
        android:background="@drawable/round"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:padding="5dip" >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/sun" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:layout_marginLeft="15dip" >

                <ImageView
                    android:id="@+id/rupee"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/rupee" />

                <TextView
                    android:id="@+id/day_amount"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical|right"
                    android:layout_marginLeft="10dip"
                    android:numeric="decimal"
                    android:text="0.0"
                    android:textColor="@color/autoText"
                    android:textSize="@dimen/text_size_medium"
                    android:textStyle="bold" />
            </LinearLayout>
        </LinearLayout>

        <View
            android:layout_width="2dip"
            android:layout_height="wrap_content"
            android:background="#ff6600" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:padding="5dip" >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/moon" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal"
                android:layout_marginLeft="15dip" >

                <ImageView
                    android:id="@+id/rupee"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/rupee" />

                <TextView
                    android:id="@+id/night_amount"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical|right"
                    android:layout_marginLeft="10dip"
                    android:numeric="decimal"
                    android:text="0.0"
                    android:textColor="@color/autoText"
                    android:textSize="@dimen/text_size_medium"
                    android:textStyle="bold" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

地图活动代码:-

// Background AsyncTask Class

    class CurrentPositionTask extends AsyncTask<String, Void, Void> {
        boolean flag = true;
        @Override
        public void onPreExecute() {
            // TODO Auto-generated method stub
            System.out.println("Executed");

                    // TODO Auto-generated method stub
                    dialog = new ProgressDialog(AutoMapActivity.this.getApplicationContext());
                    dialog.setTitle("");
                    dialog.setMessage("Searching");
                    dialog.show();
        }

        @Override
        public Void doInBackground(String... params) {
            // TODO Auto-generated method stub
            while(flag){
                //System.out.println("While Loop");
                if (latitude != 0 && longitude != 0) {
                    flag = false;
                    break;
                }
            }

            return null;
        }

        @Override
        public void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            locationManager.removeUpdates(locationListener);
            if (latitude!=0 && longitude!=0) {
                dialog.cancel();
                setInit();
            }
        }
    }

    //LocationListner Class
    class MyLocationListener implements LocationListener {

        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            if (location != null) {
                latitude = location.getLatitude();
                longitude = location.getLongitude();
            }
        }

        public void onProviderDisabled(String arg0) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "Gps Disabled",
                    Toast.LENGTH_LONG).show();

        }

        public void onProviderEnabled(String arg0) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "Gps Enabled",
                    Toast.LENGTH_SHORT).show();
        }

        public void onStatusChanged(String provider, int status, Bundle extra) {
            // TODO Auto-generated method stub
            switch (status) {
            case 0:
                // OutofService
                Toast.makeText(getApplicationContext(), "Out of Service",
                        Toast.LENGTH_LONG).show();
                break;
            case 1:
                // Temporarily unavailable
                Toast.makeText(getApplicationContext(),
                        "Location Temporary Unavailable", Toast.LENGTH_LONG)
                        .show();
                break;
            case 2:
                // Available
                Toast.makeText(getApplicationContext(), "Available",
                        Toast.LENGTH_LONG).show();
                break;
            }
        }
    }

记录猫:- '

07-17 19:37:49.267: W/System.err(881): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
07-17 19:37:49.277: W/System.err(881):  at android.view.ViewRoot.setView(ViewRoot.java:531)
07-17 19:37:49.277: W/System.err(881):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
07-17 19:37:49.277: W/System.err(881):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
07-17 19:37:49.277: W/System.err(881):  at android.app.Dialog.show(Dialog.java:241)
07-17 19:37:49.277: W/System.err(881):  at com.test.fare.AutoMapActivity$CurrentPositionTask.onPreExecute(AutoMapActivity.java:195)
07-17 19:37:49.277: W/System.err(881):  at android.os.AsyncTask.execute(AsyncTask.java:391)
07-17 19:37:49.277: W/System.err(881):  at com.test.fare.AutoMapActivity.getUserCurrentLocation(AutoMapActivity.java:146)
07-17 19:37:49.277: W/System.err(881):  at com.test.fare.AutoMapActivity.onCreate(AutoMapActivity.java:80)
07-17 19:37:49.277: W/System.err(881):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-17 19:37:49.287: W/System.err(881):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
07-17 19:37:49.287: W/System.err(881):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-17 19:37:49.287: W/System.err(881):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-17 19:37:49.287: W/System.err(881):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-17 19:37:49.287: W/System.err(881):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-17 19:37:49.287: W/System.err(881):  at android.os.Looper.loop(Looper.java:130)
07-17 19:37:49.287: W/System.err(881):  at android.app.ActivityThread.main(ActivityThread.java:3683)
07-17 19:37:49.297: W/System.err(881):  at java.lang.reflect.Method.invokeNative(Native Method)
07-17 19:37:49.297: W/System.err(881):  at java.lang.reflect.Method.invoke(Method.java:507)
07-17 19:37:49.297: W/System.err(881):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-17 19:37:49.297: W/System.err(881):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-17 19:37:49.297: W/System.err(881):  at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

14

在 MapActivity 创建对话框时尝试使用 AutoMapActivity.this 而不是 AutoMapActivity.this.getApplicationContext()?

于 2012-07-17T14:45:45.447 回答