마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

기기에 따라 오류가 다를 수 있나요?

0 추천

하나는 HTC 이보4G 라는 폰과 갤럭시플레이어 5.8 제품을 사용해서

어떤 앱을 테스트하는데 .. 

같은 앱을 실핼시키니까 .. HTC꺼는 문제없이 실행되는데 .. 

삼성 갤럭시는 널포인터익셉션이 나옵니다..

해당 로그켓과 해당 소스를 보여드리겠습니다.

 

 

public class HHRS_main extends Activity {

	private static final String TAG = HHRS_main.class.getName();
	private TextView textlocation;
	private TextView textAddress;

	LocationManager locationManager;
	LocationListener locationListener;
	Location location;
	Geocoder geoCoder;

	String messageAddress = "주소를 확인할 수 없음.";
	String messageLocation = "위치를 확인할 수 없음.";

	double latitude;
	double longitude;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		setLayout();
		myLocation();
	}

	
	/** 현재 위치 정보 시작 */
	private void myLocation() {
		// 위치 정보를 얻어옴
		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		String provider = LocationManager.GPS_PROVIDER;
		location = locationManager.getLastKnownLocation(provider);

		latitude = location.getLatitude();
		longitude = location.getLongitude();

		messageLocation = String
				.format("위도 : %f,  경도: %f", latitude, longitude);

		if (location != null) {
			textlocation.setText(messageLocation);
			textAddress.setText(getAddres());
		} else {
			textAddress.setText("주소정보를 얻지 못함.");
		}

		locationListener = new LocationListener() {
			@Override
			public void onStatusChanged(String provider, int status,
					Bundle extras) {
			}

			@Override
			public void onProviderEnabled(String provider) {
			}

			@Override
			public void onProviderDisabled(String provider) {
			}

			@Override
			public void onLocationChanged(Location location) {
				// GPS 정보가 변경되었을 경우
				// messageLocation = String.format("위도 : %f,  경도: %f", latitude,
				// longitude);

				textlocation.setText(messageLocation);
				textAddress.setText(getAddres());
			}
		};
		locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER, 0,
				0, locationListener);
	}

	private String getAddres() {
		Geocoder gc = new Geocoder(getApplicationContext(), Locale.KOREA);
		try {
			List<Address> addresses = gc
					.getFromLocation(latitude, longitude, 1);
			StringBuilder sb = new StringBuilder();

			if (null != addresses && addresses.size() > 0) {
				Address address = addresses.get(0);

				sb.append(address.getLocality() + " ");
				sb.append(address.getSubLocality() + " ");
				sb.append(address.getThoroughfare() + " ");
				sb.append(address.getFeatureName());

				messageAddress = sb.toString();
			}
		} catch (IOException e) {
			Toast.makeText(HHRS_main.this, "주소를 얻지 못함", 1000).show();
		}
		return messageAddress;
	}
	/** 현재 위치 정보 끝 */

	/** 레이아웃 시작 */	
	private void setLayout() {
		// XML 레이아웃
		TabHost tabHost = (TabHost) findViewById(R.id.tabhost);
		tabHost.setup();
		TabHost.TabSpec spec;

		spec = tabHost.newTabSpec("1");
		spec.setContent(R.id.tab1);
		spec.setIndicator("행동정보");
		tabHost.addTab(spec);

		spec = tabHost.newTabSpec("2");
		spec.setContent(R.id.tab2);
		spec.setIndicator("행동요량");
		tabHost.addTab(spec);

		spec = tabHost.newTabSpec("3");
		spec.setContent(R.id.tab3);
		spec.setIndicator("설정");
		tabHost.addTab(spec);

		textlocation = (TextView) findViewById(R.id.main_location);
		textAddress = (TextView) findViewById(R.id.main_address);
	}
	/** 레이아웃 끝 */
}

 

Hui (590 포인트) 님이 2013년 6월 20일 질문
에러가 난 줄좀 표시해주세요^^

패키지명하고 import때문에 계산이 안되요
35 라인의 latitude = location.getLatitude(); 부분입니다.
두기기중 한 기기에만 널포인트익셉션이 나오는데 .. 이해가 안갑니다..

2개의 답변

0 추천

locationManager.getLastKnownLocation(provider);

위 코드에서 null을 리턴한 것 같은데요..

android reference 를 보시면 다음과 같이 내용이 나옵니다.

Returns
  • the last known location for the provider, or null
Throws
SecurityException if no suitable permission is present
IllegalArgumentException if provider is null or doesn't exist

기본적으로 Argument가 잘못되었거나, 퍼미션에 문제가 있는 것 같은데요..

검토해 보시죠.

익명사용자 님이 2013년 6월 21일 답변
0 추천

다른분이 답글다신 location = locationManager.getLastKnownLocation(provider);

부분에서 null이 나오는게 맞는거 같네요

이건 기기에 따른 문제가 아니라 코드에 따른 문제로 보이네요

위의 코드로는 항상 location정보를 얻을 수 없습니다

api문서를 참고 하시고 만약 null이 나오는 경우 location을 값을 받기 위해

wifi를 통한 gps값이나 gps수신을 통하여 얻도록 코드를 수정하여야 할것 같네요

블랙프리 (1,050 포인트) 님이 2013년 6월 21일 답변
이분 말이 맞네요 ㅇㅅㅇ
퍼미션에러는 왠만한건 공통입니다
(개발하면서 SecurityException 거의 못봄 ㅋ)
getLastKnownLocation
이건 위치정보 마지막을 가져오는 코드입니다. .한번도 위치정보 갱신을 안해다면
널을 가져올수밖에 없는구조고요.. 널에따른 예외처리를 반드시해주셔야합니다
전에 해결했는데 맞습니다.
한 기기는 기존 GPS값이 있었고 한 기기는 GPS값이 없었습니다.
그래서 한기기가 이상이있는지 알았는데 소스상의 문제였습니다. 감사합니다.
...