简体   繁体   English

Android应用程式只能在模拟器中使用

[英]Android app working only in emulator

I have a basic android app that gets current location and time of last fix. 我有一个基本的android应用,可获取当前位置和上次修复时间。 It is divided into 3 tabs (fragments). 它分为3个选项卡(片段)。 First 2 works good on emulator and also on phone but when I try to switch to 3rd one on phone the app crashes. 前2个在模拟器和手机上都可以正常工作,但是当我尝试在手机上切换到第3个时,应用崩溃。 Here is a part of code of tab3 fragment but I think this isnt causing the problem: 这是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();
}

and here is the code of gpsHandler class (I think the problem is somewhere here but I dont know where): 这是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) {

    }
}

here is logcat: 这是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.

It looks like it has some problems with setting text of textview. 设置textview的文本似乎有些问题。 Thanks in forward for your help. 在此先感谢您的帮助。

As you see in logcat, the error happens in line 42: 如您在logcat中看到的,该错误发生在第42行:

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

So there are 2 possible exceptions that can occur in this line: 因此,此行中可能会发生2种可能的异常:

  • NullpointerException because locationManager.getLastKnownLocation(latestFixProvider) returns null. NullpointerException,因为locationManager.getLastKnownLocation(latestFixProvider)返回null。 You should catch this 你应该抓住这个

  • the locationManager.getLastKnownLocation() call could be the reason for the force close, because you specified an invalid parameter. 由于您指定了无效的参数,所以locationManager.getLastKnownLocation()调用可能是强制关闭的原因。

Most likely: See line 31, bad style: String latestFixProvider = new String(); 最有可能的情况:参见第31行,风格较差:String lastFixProvider = new String();

If the for-loop in line 33 doesn't find a latestFixProvider, you will call the method with an empty String. 如果第33行中的for循环未找到latestFixProvider,则将使用空String调用该方法。 This could be the reason for the force close. 这可能是强制关闭的原因。

Better: 更好:

    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: instead of String foobar = new String(); PS:代替String foobar = new String(); you should use String foobar = ""; 您应该使用String foobar =“”;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM