2013-08-01 42 views
-1

我對Android開發非常陌生。所以請原諒這個重複的問題。我已經嘗試了很多這方面的解決方案,但似乎沒有一個適合我......他們都在模擬器和手機上崩潰。試圖獲取GPS位置並在TextView中顯示

我正在研究一個應用程序,只想獲取我的GPS位置,然後將TextView設置爲'big'來設置該位置的值。

繼承人的代碼。 (我在我的code..I導入語句,一切都只是didnt將其複製過來

GPSActivity //啓動活動。TextView的R.id.big確實存在

public class GPSActivity extends Activity{ 

private LocationManager  manager = null; 
private MyLocationListener listener; 
private TextView    textView; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


    listener = new MyLocationListener(textView); 
    textView = (TextView) findViewById(R.id.big); 

    manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

    listener = new MyLocationListener(textView); 

    manager.requestLocationUpdates(LocationManager 
      .GPS_PROVIDER, 5000, 10,listener); 

    } 
} 

我實現我的自己LocationListener的改變我的TextView ...我不知道如果我真的允許做這個....

public class MyLocationListener implements LocationListener 
{ 
TextView textView; 

public MyLocationListener(TextView textView) 
{ 
    this.textView = textView; 
} 

@Override 
public void onLocationChanged(Location location) { 
    this.textView.setText(location.toString()); 
} 

@Override 
public void onStatusChanged(String s, int i, Bundle bundle) { 

} 

@Override 
public void onProviderEnabled(String s) { 

} 

@Override 
public void onProviderDisabled(String s) { 

} 
} 

最後我的清單文件。我確信,包括我所需要的權限。

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.mytestapp" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="12" 
    android:targetSdkVersion="16" /> 
<application 

    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@android:style/Theme.NoTitleBar" > 
    <activity 
     android:name="com.mytestapp.GPSActivity" 
     android:permission="ACCESS_FINE_LOCATION" 
     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> 

</manifest> 

當我撥打requestLocationUpdates()時,問題發生在GPSActivity。如果我在代碼中保留此行,該應用會崩潰。如果我將它評論出來,TextView的「大」就像我期望的那樣顯示。

編輯:我也試着創造一個LocationListener作爲一個內部類就像我已經看到了很多其他人的對等做......和我有同樣的結果(應用程序崩潰)

+1

Praytell,那麼你的logcat的說關於崩潰? – 323go

+0

Hmmmm.good問題我甚至沒有想過要檢查。在模擬器上它說 'java.lang.RuntimeException:無法啓動活動ComponentInfo {com.testapp/com.testapp.GPSActivity}:java.lang.SecurityException:「gps」位置提供程序需要ACCESS_FINE_LOCATION權限.' 所以。 ...我的清單文件有什麼問題? – Rell3oT

+0

關於logcat的好處是,它告訴你在很多情況下到底出了什麼問題:「位置提供者需要ACCESS_FINE_LOCATION權限」。添加它。 – 323go

回答

1

添加

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 

添加到您的manifest.xml文件。

+0

好的...謝謝。我的位置沒有顯示,但至少我沒有崩潰。看起來我有一些其他問題需要弄清楚。謝謝! – Rell3oT

0

在除了ACCESS_FINE_LOCATION允許嘗試加入 ACCESS_GPS

,他們應該是這樣的......

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_GPS" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
0

進行地理定位的完整支持使用GPS或WiFi使用:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
<uses-permission android:name="android.permission.ACCESS_GPS" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.INTERNET" /> 

併爲鴕鳥政策有支持GPS的設備使用:

<uses-feature 
    android:name="android.hardware.location.gps" 
    android:required="false" />