2014-10-20 46 views
1

我有一個基本的Android應用程序,它獲取當前位置和最後一次修復的時間。它被分成3個標籤(片段)。前2在模擬器和手機上都能很好地工作,但是當我嘗試切換到手機上的第3個時,應用程序崩潰。這裏是TAB3片段的代碼的一部分,但我覺得這個心不是造成問題的原因:Android應用程序只能在模擬器中工作

public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    TextView Latitude = (TextView) getView().findViewById(R.id.txtLat); 
    TextView Longitude = (TextView) getView().findViewById(R.id.txtLong); 
    TextView Status = (TextView) getView().findViewById(R.id.txtStatus); 
    gpsHandler gpsHandler = new gpsHandler((LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE), Longitude ,Latitude, Status); 
    gpsHandler.lastFix(); 
} 

,這裏是gpsHandler類的代碼(我認爲這個問題是這裏的某個地方,但我不知道在哪裏):

package hzs.sk.hzs; 

import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.widget.TextView; 

import java.text.SimpleDateFormat; 
import java.util.Calendar; 
import java.util.List; 
import java.util.TimeZone; 

/** 
* Created by Daniel on 19.10.2014. 
*/ 
public class gpsHandler implements LocationListener { 
    protected LocationManager locationManager; 
    protected LocationListener locationListener; 
    TextView TWlongitude, TWlatitude, TWstatus; 
    public gpsHandler(LocationManager locationManager, TextView TWlongitude, TextView TWlatitude, TextView TWstatus){ 
     this.locationManager = locationManager; 
     this.TWlongitude = TWlongitude; 
     this.TWlatitude = TWlatitude; 
     this.TWstatus = TWstatus; 
     //GPS provider, minRefreshTime, minRefreshDistance, locationListener 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 5, this); 
    } 
    public void lastFix(){ 
     List<String> providers = locationManager.getAllProviders(); 
     String latestFixProvider = new String(); 
     long time = 0; 
     for(int i = 0; i < providers.size(); i++){ 
      Location lastKnown = locationManager.getLastKnownLocation(providers.get(i)); 
      if(lastKnown != null && lastKnown.getTime() > time){ 
       latestFixProvider = providers.get(i); 
       time = lastKnown.getTime(); 
      } 
     } 
     String lastFixDate = getDate(time, "dd/MM/yyyy hh:mm:ss.SSS"); 

     TWlongitude.setText(Double.toString(locationManager.getLastKnownLocation(latestFixProvider).getLongitude())); 
     TWlatitude.setText(Double.toString(locationManager.getLastKnownLocation(latestFixProvider).getLatitude())); 
     TWstatus.setText(lastFixDate); 
    } 
    private static String getDate(long milliSeconds, String dateFormat) 
    { 
     // Create a DateFormatter object for displaying date in specified format. 
     SimpleDateFormat formatter = new SimpleDateFormat(dateFormat); 

     // Create a calendar object that will convert the date and time value in milliseconds to date. 
     Calendar calendar = Calendar.getInstance(); 
     TimeZone tz = calendar.getTimeZone(); 
     formatter.setTimeZone(tz); 
     calendar.setTimeInMillis(milliSeconds); 
     return formatter.format(calendar.getTime()); 
    } 
    @Override 
    public void onLocationChanged(Location location) { 
     TWlongitude.setText(Double.toString(location.getLongitude())); 
     TWlatitude.setText(Double.toString(location.getLatitude())); 
     long time= System.currentTimeMillis(); 
     TWstatus.setText(getDate(time, "dd/MM/yyyy hh:mm:ss.SSS")); 
    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 

    } 

    @Override 
    public void onProviderEnabled(String provider) { 

    } 

    @Override 
    public void onProviderDisabled(String provider) { 

    } 
} 

這裏是logcat的:

10-20 08:11:08.007 I/ActivityManager(2044): Force stopping package hzs.sk.hzs uid=10036 
10-20 08:11:23.292 E/AndroidRuntime(25044):  at hzs.sk.hzs.gpsHandler.lastFix(gpsHandler.java:42) 
10-20 08:11:23.292 E/AndroidRuntime(25044):  at hzs.sk.hzs.tab3.onActivityCreated(tab3.java:55) 
10-20 08:11:31.349 E/ActivityManager(2044): App already has crash dialog: ProcessRecord{44487f30 25044:hzs.sk.hzs/u0a36} 
10-20 08:11:34.773 I/ActivityManager(2044): Process hzs.sk.hzs (pid 25044) has died. 
10-20 08:12:17.065 I/ConfigFetchService(21453): PackageReceiver: Intent { act=android.intent.action.PACKAGE_ADDED dat=package:hzs.sk.hzs flg=0x8000010 cmp=com.google.android.gms/.config.ConfigFetchService$PackageReceiver (has extras) } 
10-20 08:12:17.075 I/ConfigFetchService(21453): onStartCommand Intent { act=android.intent.action.PACKAGE_ADDED dat=package:hzs.sk.hzs cmp=com.google.android.gms/.config.ConfigFetchService (has extras) } 
10-20 08:12:17.616 D/PackageBroadcastService(21453): Received broadcast action=android.intent.action.PACKAGE_ADDED and uri=hzs.sk.hzs 
10-20 08:13:37.455 E/AndroidRuntime(26655):  at hzs.sk.hzs.gpsHandler.lastFix(gpsHandler.java:42) 
10-20 08:13:37.455 E/AndroidRuntime(26655):  at hzs.sk.hzs.tab3.onActivityCreated(tab3.java:55) 
10-20 08:13:37.455 E/AndroidRuntime(26655):  at hzs.sk.hzs.MainActivity.onTabSelected(MainActivity.java:103) 
10-20 08:13:44.993 E/ActivityManager(2044): App already has crash dialog: ProcessRecord{44658fc0 26655:hzs.sk.hzs/u0a36} 
10-20 08:13:46.835 I/ActivityManager(2044): Process hzs.sk.hzs (pid 26655) has died. 

它看起來像它有一些問題設定的TextView的文本。感謝您的幫助。

+1

發佈您的logcat然後.... – 2014-10-20 06:02:45

+0

對不起,我忘了它。我現在添加了它 – horin 2014-10-20 06:18:17

回答

0

正如在logcat中看到的,誤差在線路42發生的情況:

TWlongitude.setText(Double.toString(locationManager.getLastKnownLocation(latestFixProvider).getLongitude()));

因此,有可能出現在這一行2個可能的例外:

  • NullPointerException異常,因爲locationManager.getLastKnownLocation(latestFixProvider)返回null。你應該注意到這個

  • 因爲你指定了一個無效的參數,locationManager.getLastKnownLocation()調用可能是強制關閉的原因。

最有可能的: 見31行不良作風:字符串latestFixProvider =新的String();

如果第33行中的for循環沒有找到latestFixProvider,那麼您將使用空字符串調用該方法。這可能是部隊關閉的原因。

更好:PS

String latestFixProvider = null; 
    long time = 0; 
    for(int i = 0; i < providers.size(); i++){ 
     Location lastKnown = locationManager.getLastKnownLocation(providers.get(i)); 
     if(lastKnown != null && lastKnown.getTime() > time){ 
      latestFixProvider = providers.get(i); 
      time = lastKnown.getTime(); 
     } 
    } 
    String lastFixDate = getDate(time, "dd/MM/yyyy hh:mm:ss.SSS"); 

    if (latestFixProvider != null) { 
     Location loc = locationManager.getLastKnownLocation(latestFixProvider); 
     if (loc != null) { 
      TWlongitude.setText(Double.toString(loc.getLongitude())); 
      TWlatitude.setText(Double.toString(loc.getLatitude())); 
     } 
    } 

:代替 字符串foobar的新=字符串();你應該使用 字符串foobar =「」;

相關問題