应用程序在 Android 模拟器中意外停止

发布于 2024-11-03 11:58:23 字数 6466 浏览 3 评论 0原文

我正在寻找获取用户位置的优化方法,我在这里找到了示例示例,Fedor 先生于 2010 年 6 月 30 日回答了这个问题,

我按照他在代码中解释的方式做了同样的事情,唯一的区别是我我使用抽象类结果的gotLocation回调方法。在此方法中,我尝试使用 Toast.makeText 将提供程序名称显示为消息。当我运行此代码时,我的模拟器上没有显示任何内容,几秒钟后它显示消息“应用程序已意外停止 android 模拟器”。我增加了在timer1.schedule方法中设置的时间,但没有运气。

我只是在android平台上进行开发,所以我对此没有足够的了解,所以任何人都可以帮助我解决这个问题。

下面是我的代码

文件名:UserLocation.java

package com.ideafarms.android.mylocation;

import java.util.Timer;
import java.util.TimerTask;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class UserLocation{
    Timer timer1;
    LocationManager locMgr;
    LocationResult locationResult;
    boolean gps_enabled = false;
    boolean network_enabled = false;

    LocationListener locationListenerGps = new LocationListener() {

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            timer1.cancel();
            locationResult.gotLocation(location);
            locMgr.removeUpdates(this);
            locMgr.removeUpdates(locationListenerNetwork);
        }
    };

    LocationListener locationListenerNetwork = new LocationListener() {

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            timer1.cancel();
            locationResult.gotLocation(location);
            locMgr.removeUpdates(this);
            locMgr.removeUpdates(locationListenerGps);          
        }
    };

    public boolean getLocation(Context context, LocationResult result){
        // Use LocationResult callback class to pass location value from UserLocation to User code
        locationResult = result;
        if(locMgr == null){
            locMgr = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
            // Handle exception if provider is not permitted
            try{
                gps_enabled = locMgr.isProviderEnabled(LocationManager.GPS_PROVIDER);
            }catch(Exception ex){

            }
            try{
                network_enabled = locMgr.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
            }catch(Exception ex){

            }
            // don't start listeners if no provider is enabled
            if(!gps_enabled && !network_enabled){
                return false;
            }

            if(gps_enabled){                
                locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
            }
            if(network_enabled){                
                locMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
            }
            timer1 = new Timer();
            timer1.schedule(new GetLastLocation(), 20000);
            return true;
        }
        return true;
    }
    class GetLastLocation extends TimerTask {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            locMgr.removeUpdates(locationListenerGps);
            locMgr.removeUpdates(locationListenerNetwork);
            Location net_loc=null, gps_loc=null;
            if(gps_enabled){
                gps_loc = locMgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);

            }
            if(network_enabled){
                net_loc=locMgr.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

            }
            // if there are both values use the latest one
            if(gps_loc!=null && net_loc!= null){
                if(gps_loc.getTime()>net_loc.getTime()){
                    locationResult.gotLocation(gps_loc);
                }else{
                    locationResult.gotLocation(net_loc);
                    }
                return;
            }
            if(gps_loc!=null){
                locationResult.gotLocation(gps_loc);
                return;
            }
            if(net_loc!=null){
                locationResult.gotLocation(net_loc);
                return;
            }
            locationResult.gotLocation(null);
        }

    }
    public static abstract class LocationResult{
        public abstract void gotLocation(Location location);
    }
}

package com.ideafarms.android.mylocation;

import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import com.ideafarms.android.mylocation.UserLocation.LocationResult;

public class MyLocation extends Activity {

    /** Called when the activity is first created. */
    TextView myLoc ;
    public LocationResult locResult = new LocationResult(){
        @Override
        *public void gotLocation(Location location) {
            // TODO Auto-generated method stub

            Toast msg = Toast.makeText(MyLocation.this, location.getProvider(), Toast.LENGTH_LONG);
            //msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg.getYOffset() / 2);
            msg.show();
        }*      
    };


    boolean loc;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        UserLocation usrLocation = new UserLocation();
        myLoc = (TextView)findViewById(R.id.myLocation);
        loc = usrLocation.getLocation(this, locResult);

    }

}

我已将遇到问题的代码用斜体标记。

谢谢

I am looking for the optimized way to get user's location and I found the sample example here, which was answered by Mr. Fedor on on Jun 30 '10

I did the same way as he explained in his code, the only difference is that I am using the gotLocation callback method of abstract class result. In this method I am tring to show the Provider name as a msg using Toast.makeText. When I run this code, nothing get displayed on my emulator and after few seconds it show the message "Application has stopped unexpectedly android emulator". I increase the time, which was set in the timer1.schedule method, but no luck.

I am just stating development in android platform, so I don't have enough knowledge about the same, so can anybody help me to resolve this issue.

Below is my code

file Name: UserLocation.java

package com.ideafarms.android.mylocation;

import java.util.Timer;
import java.util.TimerTask;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class UserLocation{
    Timer timer1;
    LocationManager locMgr;
    LocationResult locationResult;
    boolean gps_enabled = false;
    boolean network_enabled = false;

    LocationListener locationListenerGps = new LocationListener() {

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            timer1.cancel();
            locationResult.gotLocation(location);
            locMgr.removeUpdates(this);
            locMgr.removeUpdates(locationListenerNetwork);
        }
    };

    LocationListener locationListenerNetwork = new LocationListener() {

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            timer1.cancel();
            locationResult.gotLocation(location);
            locMgr.removeUpdates(this);
            locMgr.removeUpdates(locationListenerGps);          
        }
    };

    public boolean getLocation(Context context, LocationResult result){
        // Use LocationResult callback class to pass location value from UserLocation to User code
        locationResult = result;
        if(locMgr == null){
            locMgr = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
            // Handle exception if provider is not permitted
            try{
                gps_enabled = locMgr.isProviderEnabled(LocationManager.GPS_PROVIDER);
            }catch(Exception ex){

            }
            try{
                network_enabled = locMgr.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
            }catch(Exception ex){

            }
            // don't start listeners if no provider is enabled
            if(!gps_enabled && !network_enabled){
                return false;
            }

            if(gps_enabled){                
                locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
            }
            if(network_enabled){                
                locMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
            }
            timer1 = new Timer();
            timer1.schedule(new GetLastLocation(), 20000);
            return true;
        }
        return true;
    }
    class GetLastLocation extends TimerTask {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            locMgr.removeUpdates(locationListenerGps);
            locMgr.removeUpdates(locationListenerNetwork);
            Location net_loc=null, gps_loc=null;
            if(gps_enabled){
                gps_loc = locMgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);

            }
            if(network_enabled){
                net_loc=locMgr.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

            }
            // if there are both values use the latest one
            if(gps_loc!=null && net_loc!= null){
                if(gps_loc.getTime()>net_loc.getTime()){
                    locationResult.gotLocation(gps_loc);
                }else{
                    locationResult.gotLocation(net_loc);
                    }
                return;
            }
            if(gps_loc!=null){
                locationResult.gotLocation(gps_loc);
                return;
            }
            if(net_loc!=null){
                locationResult.gotLocation(net_loc);
                return;
            }
            locationResult.gotLocation(null);
        }

    }
    public static abstract class LocationResult{
        public abstract void gotLocation(Location location);
    }
}

and

package com.ideafarms.android.mylocation;

import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import com.ideafarms.android.mylocation.UserLocation.LocationResult;

public class MyLocation extends Activity {

    /** Called when the activity is first created. */
    TextView myLoc ;
    public LocationResult locResult = new LocationResult(){
        @Override
        *public void gotLocation(Location location) {
            // TODO Auto-generated method stub

            Toast msg = Toast.makeText(MyLocation.this, location.getProvider(), Toast.LENGTH_LONG);
            //msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg.getYOffset() / 2);
            msg.show();
        }*      
    };


    boolean loc;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        UserLocation usrLocation = new UserLocation();
        myLoc = (TextView)findViewById(R.id.myLocation);
        loc = usrLocation.getLocation(this, locResult);

    }

}

I have marked the code in italic where I am having problem.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执笏见 2024-11-10 11:58:23

看一下这里: ​​http://developer.android.com/guide/developing/ tools/emulator.html

您需要模拟 GPS 设备才能从模拟器获取位置数据。

Take a look here: http://developer.android.com/guide/developing/tools/emulator.html

You need to emulate an gps device in order to get location data from the emulator.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文