Android应用仅在模拟器中工作

时间:2014-10-20 06:01:47

标签: android gps emulation

我有一个基本的Android应用程序,它获取当前位置和上次修复的时间。它分为3个选项卡(片段)。前2个在模拟器和手机上运行良好,但当我尝试在手机上切换到第3个时,应用程序崩溃了。这是tab3片段代码的一部分,但我认为这不会导致问题:

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 个答案:

答案 0 :(得分:0)

正如您在logcat中看到的那样,错误发生在第42行:

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

因此,此行中可能发生两种可能的异常:

  • NullpointerException,因为locationManager.getLastKnownLocation(latestFixProvider)返回null。你应该抓住这个

  • locationManager.getLastKnownLocation()调用可能是强制关闭的原因,因为您指定了无效参数。

最有可能: 参见第31行,错误样式:String latestFixProvider = new String();

如果第33行中的for循环找不到latestFixProvider,则将使用空String调用该方法。这可能是部队关闭的原因。

更好:

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

PS:而不是  String foobar = new String(); 你应该使用  String foobar =“”;