안드로이드 개발 질문/답변
(글 수 45,052)
여기저기에서 도움을 받아 GPS좌표 구하는 프로그램은 성공을 했습니다.
그런데 다른 CLASS에서 GPS좌표만 얻어오려는데 getSystemService는 Activity가 시작되어야만
가능하다는 오류가 나와서 방법을 찾고 있는중입니다. 도움 좀 부탁드립니다.
그리고 버튼클릭시 동기화 형태로 Activity가 실행될수 있나요?
그런데 다른 CLASS에서 GPS좌표만 얻어오려는데 getSystemService는 Activity가 시작되어야만
가능하다는 오류가 나와서 방법을 찾고 있는중입니다. 도움 좀 부탁드립니다.
그리고 버튼클릭시 동기화 형태로 Activity가 실행될수 있나요?
2010.04.12 13:50:43
답변 갑사드립니다. 그런데 오류 메세지는
04-12 22:33:37.910: ERROR/AndroidRuntime(363): Uncaught handler: thread main exiting due to uncaught exception
04-12 22:33:37.930: ERROR/AndroidRuntime(363): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.access.bml/com.access.bml.BMLAndroidStdApi}: java.lang.IllegalStateException: System services not available to Activities before onCreate()
04-12 22:33:37.930: ERROR/AndroidRuntime(363): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
04-12 22:33:37.930: ERROR/AndroidRuntime(363): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
04-12 22:33:37.930: ERROR/AndroidRuntime(363): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
04-12 22:33:37.930: ERROR/AndroidRuntime(363): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
04-12 22:33:37.930: ERROR/AndroidRuntime(363): at android.os.Handler.dispatchMessage(Handler.java:99)
04-12 22:33:37.930: ERROR/AndroidRuntime(363): at android.os.Looper.loop(Looper.java:123)
04-12 22:33:37.930: ERROR/AndroidRuntime(363): at android.app.ActivityThread.main(ActivityThread.java:4363)
04-12 22:33:37.930: ERROR/AndroidRuntime(363): at java.lang.reflect.Method.invokeNative(Native Method)
04-12 22:33:37.930: ERROR/AndroidRuntime(363): at java.lang.reflect.Method.invoke(Method.java:521)
04-12 22:33:37.930: ERROR/AndroidRuntime(363): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-12 22:33:37.930: ERROR/AndroidRuntime(363): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-12 22:33:37.930: ERROR/AndroidRuntime(363): at dalvik.system.NativeStart.main(Native Method)
04-12 22:33:37.930: ERROR/AndroidRuntime(363): Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
04-12 22:33:37.930: ERROR/AndroidRuntime(363): at android.app.Activity.getSystemService(Activity.java:3468)
04-12 22:33:37.930: ERROR/AndroidRuntime(363): at com.access.bml.BMLAndroidStdApi.BML_CB_XDPA_GetCurPosPeer(BMLAndroidStdApi.java:53)
04-12 22:33:37.930: ERROR/AndroidRuntime(363): at com.access.bml.BMLView.BML_CB_XDPA_GetCurPosPeer(BMLView.java:1024)
04-12 22:33:37.930: ERROR/AndroidRuntime(363): at com.access.bml.BMLAndroidStdApi.onCreate(BMLAndroidStdApi.java:38)
04-12 22:33:37.930: ERROR/AndroidRuntime(363): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-12 22:33:37.930: ERROR/AndroidRuntime(363): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
04-12 22:33:37.930: ERROR/AndroidRuntime(363): ... 11 more
이런 오류입니다.
그리고 caller가 Activity가 아닙니다. JNI테스트 중인데요 C++에서 안드로이드 GPS정보를 사용하려는것인데...테스트 방법이 없어서
아래 소스 같이 구성해서 테스트하려합니다.
-----TestActivity.java -----
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
...
Button startGPS = (Button)findViewById(R.id.startGPS);
startGPS.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(TestActivity.this, TestApi.class);
startActivity(intent);
}
});
}
------TestApi.java---------
public class TestApi extends Activity implements LocationListener {
/** Called when the activity is first created. */
// Log type
private TestView view;
double lat = 0;
double lng = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
view = new TestView(this);
TestInfo info = new TestInfo();
info = view.GetCurPos(0);
finish();
}
public TestInfo GetCurPos() {
TestInfo info = new TestInfo();
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(context);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
info = updateWithNewLocation(location);
locationManager.requestLocationUpdates(provider, 2000, 10, locationListener);
return info;
}
private TestInfo updateWithNewLocation(Location location) {
TestInfo info = new TestInfo();
if (location != null) {
lat = location.getLatitude();
lng = location.getLongitude();
info.fLongitude = makeLpadWithZero(new Double(lat).toString(),12);
info.fLatitude = makeLpadWithZero(new Double(lng).toString(),11);
} else {
info.fLongitude = "";
info.fLatitude = "";
}
return info;
}
}
---TestView.java----
public class TestView {
public TestInfo GetCurPos(int in_pos_info)
{
TestInfo info = new TestInfo();
TestApi api = new TestApi();
info = api.GetCurPos(in_pos_info);
return info;
}
}
04-12 22:33:37.910: ERROR/AndroidRuntime(363): Uncaught handler: thread main exiting due to uncaught exception
04-12 22:33:37.930: ERROR/AndroidRuntime(363): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.access.bml/com.access.bml.BMLAndroidStdApi}: java.lang.IllegalStateException: System services not available to Activities before onCreate()
04-12 22:33:37.930: ERROR/AndroidRuntime(363): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
04-12 22:33:37.930: ERROR/AndroidRuntime(363): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
04-12 22:33:37.930: ERROR/AndroidRuntime(363): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
04-12 22:33:37.930: ERROR/AndroidRuntime(363): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
04-12 22:33:37.930: ERROR/AndroidRuntime(363): at android.os.Handler.dispatchMessage(Handler.java:99)
04-12 22:33:37.930: ERROR/AndroidRuntime(363): at android.os.Looper.loop(Looper.java:123)
04-12 22:33:37.930: ERROR/AndroidRuntime(363): at android.app.ActivityThread.main(ActivityThread.java:4363)
04-12 22:33:37.930: ERROR/AndroidRuntime(363): at java.lang.reflect.Method.invokeNative(Native Method)
04-12 22:33:37.930: ERROR/AndroidRuntime(363): at java.lang.reflect.Method.invoke(Method.java:521)
04-12 22:33:37.930: ERROR/AndroidRuntime(363): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-12 22:33:37.930: ERROR/AndroidRuntime(363): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-12 22:33:37.930: ERROR/AndroidRuntime(363): at dalvik.system.NativeStart.main(Native Method)
04-12 22:33:37.930: ERROR/AndroidRuntime(363): Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
04-12 22:33:37.930: ERROR/AndroidRuntime(363): at android.app.Activity.getSystemService(Activity.java:3468)
04-12 22:33:37.930: ERROR/AndroidRuntime(363): at com.access.bml.BMLAndroidStdApi.BML_CB_XDPA_GetCurPosPeer(BMLAndroidStdApi.java:53)
04-12 22:33:37.930: ERROR/AndroidRuntime(363): at com.access.bml.BMLView.BML_CB_XDPA_GetCurPosPeer(BMLView.java:1024)
04-12 22:33:37.930: ERROR/AndroidRuntime(363): at com.access.bml.BMLAndroidStdApi.onCreate(BMLAndroidStdApi.java:38)
04-12 22:33:37.930: ERROR/AndroidRuntime(363): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-12 22:33:37.930: ERROR/AndroidRuntime(363): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
04-12 22:33:37.930: ERROR/AndroidRuntime(363): ... 11 more
이런 오류입니다.
그리고 caller가 Activity가 아닙니다. JNI테스트 중인데요 C++에서 안드로이드 GPS정보를 사용하려는것인데...테스트 방법이 없어서
아래 소스 같이 구성해서 테스트하려합니다.
-----TestActivity.java -----
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
...
Button startGPS = (Button)findViewById(R.id.startGPS);
startGPS.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(TestActivity.this, TestApi.class);
startActivity(intent);
}
});
}
------TestApi.java---------
public class TestApi extends Activity implements LocationListener {
/** Called when the activity is first created. */
// Log type
private TestView view;
double lat = 0;
double lng = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
view = new TestView(this);
TestInfo info = new TestInfo();
info = view.GetCurPos(0);
finish();
}
public TestInfo GetCurPos() {
TestInfo info = new TestInfo();
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(context);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
info = updateWithNewLocation(location);
locationManager.requestLocationUpdates(provider, 2000, 10, locationListener);
return info;
}
private TestInfo updateWithNewLocation(Location location) {
TestInfo info = new TestInfo();
if (location != null) {
lat = location.getLatitude();
lng = location.getLongitude();
info.fLongitude = makeLpadWithZero(new Double(lat).toString(),12);
info.fLatitude = makeLpadWithZero(new Double(lng).toString(),11);
} else {
info.fLongitude = "";
info.fLatitude = "";
}
return info;
}
}
---TestView.java----
public class TestView {
public TestInfo GetCurPos(int in_pos_info)
{
TestInfo info = new TestInfo();
TestApi api = new TestApi();
info = api.GetCurPos(in_pos_info);
return info;
}
}
그리고, getSystemService는 Activity가 아닌 Context에서 얻는건데요?? caller에서 보낼때 Context를 보내주는게 맞을 것 같습니다.
아마 위에서 나온 오류는 GPS 좌표를 얻어주는 클래스가 extends Activity를 안해서 생기는 문제로 보이는데, 굳이 extends Activity를 하지 않아도 되는 클래스니 앞에 설명드린대로 Context를 주입해서 하심 됩니다.