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> 

MapActivity 코드 : - - :

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

LOG 고양이 : - 여기

는 레이아웃 코드 '

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) 

답변

13

MapActivity에서 대화 상자를 만들 때 AutoMapActivity.this.getApplicationContext() 대신 AutoMapActivity.this를 사용 하시겠습니까?

+0

좋습니다. 그것의 일 벌금. getApplicationContext()를 사용할 때 왜 에러가 발생하는지 말해 주시겠습니까 ?? – Scorpion

+5

대화 상자는 전체 응용 프로그램이 아닌이 특정 활동과 관련이 있습니다. 일반적으로 범위가 활동 인 객체를 만들 때 Activity.this를 사용하십시오. 현재 활동 범위를 벗어나는 범위의 개체를 만들 때 응용 프로그램 컨텍스트를 사용하십시오. –

+0

흠. 정보를 제공해 주셔서 감사합니다. 나는이 개념을 알고 있었지만 이것을 생각하지 않았습니다. 다시 한번 감사드립니다 ... – Scorpion