주요 활동 OnCreate 메서드에서 다른 클래스로 작업을 오프로드하는 데 문제가있어서 무거운 물건을 들어 올리는 데 어려움이 있습니다.
비 활동 클래스에서 getSystemService를 호출하려고하면 예외가 발생합니다.
어떤 도움이라도 대단히 감사하겠습니다 :)
lmt.java :
package com.atClass.lmt;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.location.Location;
public class lmt extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
fyl lfyl = new fyl();
Location location = lfyl.getLocation();
String latLongString = lfyl.updateWithNewLocation(location);
TextView myLocationText = (TextView)findViewById(R.id.myLocationText);
myLocationText.setText("Your current position is:\n" + latLongString);
}
}
fyl.java
package com.atClass.lmt;
import android.app.Activity;
import android.os.Bundle;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import android.content.Context;
public class fyl {
public Location getLocation(){
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(context);
String provider = LocationManager.GPS_PROVIDER;
Location location = locationManager.getLastKnownLocation(provider);
return location;
}
public String updateWithNewLocation(Location location) {
String latLongString;
if (location != null){
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "Lat:" + lat + "\nLong:" + lng;
}else{
latLongString = "No Location";
}
return latLongString;
}
}
2
질문에 예외 및 스택 추적을 추가하면 잠재적 인 답변자에게 도움이 될 수 있습니다.
—
Bert F
안녕하세요, 실수해서 죄송합니다. 이 클래스로 Activity 클래스를 확장하려고하면 예외가 발생합니다. 이 클래스의 Activity 클래스를 확장하고 싶지 않고 getLocation 메소드 내에서 getSystemService를 호출 할 수 있기를 원합니다. 예외 : java.lang.IllegalStateException :에서 onCreate () 이전 활동에 사용할 수없는 시스템 서비스
—
케빈 파커