[問題] GPS程式問題
今天有打一個用基地台定位的手機程式
目前只有在模擬器上跑過
可是 只要跑到append那裡似乎就會出錯
出錯的意思就是跑出"sorry"的那個視窗
把那部份註解掉程式可跑
以下是完整程式碼
.java
//*************************************//
package com.demo.android.mylocation;
import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;
import android.location.Criteria;
import android.util.Log;
public class MyLocation extends Activity implements LocationListener{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
updateStat();
}
private LocationManager mgr;
private String best;
private void updateStat() {
// TODO Auto-generated method stub
mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
best = mgr.getBestProvider(criteria, true);
Location location = mgr.getLastKnownLocation(best);
StringBuffer msg = new StringBuffer();
msg.append("Latitude: ");
//有問題部份 //msg.append(Double.toString(location.getLatitude())); //**
msg.append(", Longitude: ");
//有問題部份 //msg.append(Double.toString(location.getLongitude())); //**
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
Toast.makeText(this, location.toString(), Toast.LENGTH_LONG).show();
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
protected void onResume() {
super.onResume();
mgr.requestLocationUpdates(best, 60000, 1, this);
}
@Override
protected void onPause(){
super.onPause();
mgr.removeUpdates(this);
}
}
還有一個部份 (有改動的)Manifest
//*************************************//
主要改動部份:(都是新增)
android.permission.ACCESS_COARSE_LOCATION
android.permission.ACCESS_FINE_LOCATION
android.permission.INTERNET
//*************************************//
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.demo.android.mylocation"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".MyLocation"
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="3" />
<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>
其他部分是沒有改動的
一行一行試的結果確定是append那裡出問題
append()直接給予字串不會有錯
可是在裡面引用其他函數或別的東西
就會出現問題 而去論壇有找過 好向也沒有相關文章
都是找到中文出勘誤的= =
有哪位神手大大可以給建議嗎?
感激不盡
PS:版面有點亂<(_ _)>
剛剛又試了一下發現.getLatitude()這類的函數一用就會出錯
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 203.64.183.231
→
08/18 19:16, , 1F
08/18 19:16, 1F
→
08/18 19:17, , 2F
08/18 19:17, 2F
※ 編輯: skywillnosky 來自: 203.64.183.231 (08/18 19:33)
推
08/18 19:52, , 3F
08/18 19:52, 3F
→
08/18 20:00, , 4F
08/18 20:00, 4F
→
08/18 21:26, , 5F
08/18 21:26, 5F
→
08/22 22:26, , 6F
08/22 22:26, 6F
推
08/25 16:16, , 7F
08/25 16:16, 7F
推
10/28 00:35, , 8F
10/28 00:35, 8F
Eclipse 近期熱門文章
PTT數位生活區 即時熱門文章