2013-06-05 200 views
-1

我是Android新手,遇到了基本GPS信息顯示程序的困難。該程序意外停止。如果您有任何建議或意見,我將不勝感激。GPS Android程序意外停止

我使用Android開發工具在Eclipse中爲Android 2.3.3編碼。

我看到了什麼的代碼相關的部分如下,但是,爲了您的方便,該項目在這裏存檔:

http://www.filedropper.com/showdownload.php/2013-06-05t1434gps1

代碼後續的章節:

Main.java :

package com.example.gps_1; 

import android.app.Activity; 
import android.content.Intent; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.location.LocationProvider; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.TextView; 
import android.widget.Toast; 

public class Main extends Activity implements LocationListener { 
    /* This class implements LocationListener, which listens for both changes in 
    * the location of the device and changes in the status 
    * of the GPS system. 
    * */ 

    static final String tag = "Main"; // for Log 

    TextView txtInfo; 
    LocationManager lm; 
    StringBuilder sb; 
    int noOfFixes = 0; 

    /** called when the activity is first created */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     /* Have TextView display the GPS data. 
     * */ 
     txtInfo = (TextView) findViewById(R.id.textInfo); 

     /* The location manager is allows access to location and GPS status 
     * services. 
     * */ 
     lm = (LocationManager) getSystemService(LOCATION_SERVICE); 
    } 

    @Override 
    protected void onResume() { 
     /* onResume is called after onStart even if the program has not been paused. 
     * 
     * Add location listener and request updates every 1000 ms or 10 m. 
     */ 
     lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10f, this); 
     super.onResume(); 
    } 

    @Override 
    protected void onPause() { 
     /* GPS has non-negligible battery consumption.*/ 
     lm.removeUpdates(this); 
     super.onPause(); 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     Log.v(tag, "location changed"); 

     sb = new StringBuilder(512); 

     noOfFixes++; 

     /* Display some of the information in the TextView. */ 

     sb.append("number of fixes: "); 
     sb.append(noOfFixes); 
     sb.append('\n'); 
     sb.append('\n'); 

     sb.append("longitude: "); 
     sb.append(location.getLongitude()); 
     sb.append('\n'); 

     sb.append("latitude: "); 
     sb.append(location.getLatitude()); 
     sb.append('\n'); 

     sb.append("altitiude: "); 
     sb.append(location.getAltitude()); 
     sb.append('\n'); 

     sb.append("accuracy: "); 
     sb.append(location.getAccuracy()); 
     sb.append('\n'); 

     sb.append("timestamp: "); 
     sb.append(location.getTime()); 
     sb.append('\n'); 

     txtInfo.setText(sb.toString()); 
    } 

    @Override 
    public void onProviderDisabled(String provider) { 
     /* this is called if/when the GPS is disabled in settings */ 
     Log.v(tag, "disabled"); 

     /* bring up the GPS settings */ 
     Intent intent = new Intent(
       android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
     startActivity(intent); 
    } 

    @Override 
    public void onProviderEnabled(String provider) { 
     Log.v(tag, "enabled"); 
     Toast.makeText(this, "GPS enabled", Toast.LENGTH_SHORT).show(); 

    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
     /* This is called when the GPS status alters */ 
     switch (status) { 
     case LocationProvider.OUT_OF_SERVICE: 
      Log.v(tag, "status changed: out of service"); 
      Toast.makeText(this, "status changed: out of service", 
        Toast.LENGTH_SHORT).show(); 
      break; 
     case LocationProvider.TEMPORARILY_UNAVAILABLE: 
      Log.v(tag, "status changed: temporarily unavailable"); 
      Toast.makeText(this, "status changed: temporarily unavailable", 
        Toast.LENGTH_SHORT).show(); 
      break; 
     case LocationProvider.AVAILABLE: 
      Log.v(tag, "status changed: available"); 
      Toast.makeText(this, "status changed: available", 
        Toast.LENGTH_SHORT).show(); 
      break; 
     } 
    } 

    @Override 
    protected void onStop() { 
     /* upgrade: save the state */ 
     finish(); 
     super.onStop(); 
    } 
} 

的main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".Main" > 

    <TextView 
     android:id="@+id/textInfo" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/hello_world" /> 

</RelativeLayout> 
+3

把你的logcat錯誤在這裏 –

回答

1

你添加的權限清單文件

+1

請在評論中提出問題,澄清問題。不要試圖事先猜出答案。 –

+0

我認爲他沒有足夠的代表點...但任何如何它的很好的答案... –

+0

@Anshul +1老兄,但一旦你被允許評論不要寫這種類型的答覆作爲答案... –

0

添加粗位置的權限也..因爲精確的位置信息權限,只有當你的程序獲取GPS位置,如果它將從您必須添加粗的網絡位置位置權限也