2011-11-07 59 views
-1

我正在製作一個示例應用程序,用於在我的Evo 4G上定位GPS。這最終將被整合到一張時間卡使用的應用程序中。但是,它強制關閉。我沒有任何想法,爲什麼...有任何幫助?如何阻止強制關閉應用程序?

package com.Rick.GPS; 

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. */ 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    /* get TextView to display the GPS data */ 
    txtInfo = (TextView) findViewById(R.id.Text1); 

    /* the location manager is the most vital part it allows access 
    * to location and GPS status services */ 
    lm = (LocationManager) getSystemService(LOCATION_SERVICE); 
} 


protected void onResume() { 
    /* 
    * onResume is is always called after onStart, even if the app hasn't been 
    * paused 
    * 
    * add location listener and request updates every 1000ms or 10m 
    */ 
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10f, this); 
    super.onResume(); 
} 


protected void onPause() { 
    /* GPS, as it turns out, consumes battery like crazy */ 
    lm.removeUpdates(this); 
    super.onPause(); 
} 


public void onLocationChanged(Location location) { 
    Log.v(tag, "Location Changed"); 

    sb = new StringBuilder(512); 

    noOfFixes++; 

    /* display some of the data in the TextView */ 

    sb.append("No. of Fixes: "); 
    sb.append(noOfFixes); 
    sb.append('\n'); 
    sb.append('\n'); 

    sb.append("Londitude: "); 
    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()); 
} 


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); 
} 


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

} 


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; 
    } 
} 


protected void onStop() { 
    /* may as well just finish since saving the state is not important for this toy app */ 
    finish(); 
    super.onStop(); 
} 
} 

這裏是佈局文件...

<TextView 
    android:id="@+id/Text1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" /> 

這裏是清單......

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

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

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" > 
    <activity 
     android:label="@string/app_name" 
     android:name=".GPSActivity" > 
     <intent-filter > 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

</manifest> 

感謝您的幫助,您可以給我...

+1

你試過調試呢? –

+1

在日誌文件中查找異常。這應該告訴你問題出在哪裏。 (要在Eclipse中查看LogCat,請打開LogCat視圖(窗口>顯示視圖>其他...,然後選擇Android下的LogCat)。 –

+0

logcat文件中是什麼? – Nikkoli

回答

1

兩個錯誤,我可以發現:

  1. 您需要調用setContentView(R.layout.main);,不評論它。否則,在下一行中會出現NullPointerException。
  2. 在XML中,您將Text1定義爲EditText,但是在您的班級中,您嘗試將其轉換爲TextView,您會得到班級轉換異常。在XML或Java文件中更改它。根據您在代碼中的評論,您應該在XML中進行更改。
+0

謝謝,我同時改變了...仍然強制關閉日誌貓狀態:「致命的例外:主」 – user1034282

0

onLocationChange是否可以訪問textField?無論如何,我認爲設計此應用程序的更好方法是在單獨的thead中執行位置偵聽,例如,使用handler來更新UI線程。這種方法還可以消除位置修復可能會阻塞UI線程的風險。

+0

我認爲現在它...... – user1034282

0

您在onPause方法的主體中調用super.onResume。

+0

試過...沒有工作,謝謝... – user1034282

1
protected void onPause() {  /* GPS, as it turns out, consumes battery like crazy */  lm.removeUpdates(this);  super.onPause(); } 

還的setContentView代碼註釋...

+0

改變,... – user1034282

+0

請附上錯誤的詳細信息,例如強制關閉/ –