소스는 다음과 같구요...
<GPS.java>
package exam.gps; import android.app.*; import android.content.*; import android.location.*; import android.os.*; import android.widget.*; // 이 액티비티로 넘겨주면 되지만 받을 수 있는 상태가 아니다! // -> 따라서 implements LocationListener!! public class GPS extends Activity implements LocationListener{ /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 로케이션 매니저를 가져오기 위해, // 서비스에서 gps를 가져와야한다 // GPS app과 연결되어서 할당 받는다 location_manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); // 옵션 설정, 크리테리아(기준이라는 뜻) Criteria criteria = new Criteria(); // 옵션 정의 // 정확도 정의 -> NO_REQUIREMENT: 그런거 상관 없다 criteria.setAccuracy(Criteria.NO_REQUIREMENT); // PowerRequirement -> 파워 정의 (배터리, 전력 소모량) criteria.setPowerRequirement(Criteria.NO_REQUIREMENT); // Provider를 찾아야한다, GPS 위성 // String에 가장 훌륭한 위성 등을 저장한다 // 옵션 // 안 쓰는것은 필요 없다 String best_provider = location_manager.getBestProvider(criteria, true); // GPS를 가져오겠습니다~ // 지역 정보가 변경될 때 마다 알려다오! // 지금부터 위치 정보가 변경되면 알려다오! // 근데 어디다 알려주는가? // 이 액티비티로 넘겨주면 되지만 받을 수 있는 상태가 아니다! -> 따라서 implements LocationListener!! location_manager.requestLocationUpdates(best_provider, 1000, 0, this); } // 먼저, xml에 컨텐츠를 3개 열어놨으므로 (버튼1개, 텍스트뷰 2개) // 레퍼런스 선언 private TextView textview = null; private TextView tv_GeoInfo = null; private Button btn_OpenMap = null; // 로케이션 매니저 선언 private LocationManager location_manager = null; // 프로바이더 인에이블드 // 위성이나 이런 정보를 읽을 수 있는 상태가 되었음을 알려주는 메소드 (이벤트) // location_manager.requestLocationUpdates 때문에 이 메소드가 호출 되는 것 public void onProviderEnabled(String provider) { } // 상태가 변화되면 실행 public void onStatusChanged(String provider, int status, Bundle extras) { } // 우리가 쓸 가장 중요한 것 // 위치 정보가 변경 되면 호출 된다 public void onLocationChanged(Location location) { // 여기서 위치정보를 한 번 계속 찍어보자 // textview.setText(String.format("위도: %f, 경도: %f, 고도: %f", // location.getLatitude(), location.getLongitude(), location.getAltitude())); } public void onProviderDisabled(String provider) { } }
<main.xml>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:text="Open_Map" android:id="@+id/btn_OpenMap" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/TextView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="TextView없음" /> <TextView android:id="@+id/tv_GeoInfo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="tv_GeoInfo없음" /> </LinearLayout>
<GPS Mainfest>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="exam.gps" android:versionCode="1" android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".GPS" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="7" />
<use-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<use-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<use-permission android:name="android.permission.INTERNET" />
</manifest>
동영상 강의를 보면서 하는데로 쳤는데... 에러가 뜹니다;; 이유를 모르겠군요... 답변 부탁드립니다.
밑은 Logcat 데이터 입니다.
12-08 10:37:47.818: ERROR/AndroidRuntime(309): Uncaught handler: thread main exiting due to uncaught exception
12-08 10:37:47.837: ERROR/AndroidRuntime(309): java.lang.RuntimeException: Unable to start activity ComponentInfo{exam.gps/exam.gps.GPS}: java.lang.IllegalArgumentException: provider==null
12-08 10:37:47.837: ERROR/AndroidRuntime(309): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
12-08 10:37:47.837: ERROR/AndroidRuntime(309): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
12-08 10:37:47.837: ERROR/AndroidRuntime(309): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
12-08 10:37:47.837: ERROR/AndroidRuntime(309): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
12-08 10:37:47.837: ERROR/AndroidRuntime(309): at android.os.Handler.dispatchMessage(Handler.java:99)
12-08 10:37:47.837: ERROR/AndroidRuntime(309): at android.os.Looper.loop(Looper.java:123)
12-08 10:37:47.837: ERROR/AndroidRuntime(309): at android.app.ActivityThread.main(ActivityThread.java:4363)
12-08 10:37:47.837: ERROR/AndroidRuntime(309): at java.lang.reflect.Method.invokeNative(Native Method)
12-08 10:37:47.837: ERROR/AndroidRuntime(309): at java.lang.reflect.Method.invoke(Method.java:521)
12-08 10:37:47.837: ERROR/AndroidRuntime(309): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-08 10:37:47.837: ERROR/AndroidRuntime(309): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-08 10:37:47.837: ERROR/AndroidRuntime(309): at dalvik.system.NativeStart.main(Native Method)
12-08 10:37:47.837: ERROR/AndroidRuntime(309): Caused by: java.lang.IllegalArgumentException: provider==null
12-08 10:37:47.837: ERROR/AndroidRuntime(309): at android.location.LocationManager.requestLocationUpdates(LocationManager.java:613)
12-08 10:37:47.837: ERROR/AndroidRuntime(309): at exam.gps.GPS.onCreate(GPS.java:40)
12-08 10:37:47.837: ERROR/AndroidRuntime(309): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-08 10:37:47.837: ERROR/AndroidRuntime(309): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
12-08 10:37:47.837: ERROR/AndroidRuntime(309): ... 11 more