App Error 104 uncaught:运行blackberry应用程序时的运行时异常

时间:2012-03-02 05:05:00

标签: blackberry java-me geolocation gps blackberry-simulator

我在模拟器中部署代码后创建了一个使用GPS获取我的位置坐标的应用程序我遇到上述错误并附带解释 -

uncaught exception:pushmodalscreen called by a non-event thread

我无法弄清楚什么是错的。

/**
 * GPSDemo.java
 * 
 * Copyright © 1998-2011 Research In Motion Ltd.
 * 
 * Note: For the sake of simplicity, this sample application may not leverage
 * resource bundles and resource strings.  However, it is STRONGLY recommended
 * that application developers make use of the localization features available
 * within the BlackBerry development platform to ensure a seamless application
 * experience across a variety of languages and geographies.  For more information
 * on localizing your application, please refer to the BlackBerry Java Development
 * Environment Development Guide associated with this release.
 */

package com.gps;
import java.util.*;
import javax.microedition.location.*;
import net.rim.device.api.command.*;
import net.rim.device.api.gps.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.util.*;
import net.rim.blackberry.api.invoke.*;
import net.rim.blackberry.api.maps.*;
import net.rim.blackberry.api.menuitem.*;


/**
 * This application acts as a simple travel computer, recording route
 * coordinates, speed and altitude. Recording begins as soon as the
 * application is invoked.
 */
public class GPSScreen extends UiApplication
{
    // Represents the number of updates over which altitude is calculated, in seconds
    private static final int GRADE_INTERVAL = 5;
    private static final long ID = 0x5d459971bb15ae7aL;    

    // Represents period of the position query, in seconds
    private static int _interval = 1;
    private double _latitude;
    private double _longitude;    
    private LocationProvider _locationProvider;
    private GPSDemoScreen _screen;
    private MapView _mapview = new MapView();        


    /**
     * Entry point for application
     * 
     * @param args Command line arguments (not used)
     */
    public static void main(String[] args)
    {
        // Create a new instance of the application and make the currently
        // running thread the application's event dispatch thread.

        new GPSScreen().enterEventDispatcher();
    }

    /**
     * Create a new GPSDemo object
     */
    public GPSScreen()
    {
        _screen = new GPSDemoScreen();
        _screen.setTitle("GPS Demo");
        // Attempt to start the location listening thread
        if(startLocationUpdate())
        {
            _screen.setState(_locationProvider.getState());
        }

        // Render the screen
        pushScreen(_screen);
    }


    /**
     * Invokes the Location API with Standalone criteria
     * 
     * @return True if the <code>LocationProvider</code> was successfully started, false otherwise
     */
    private boolean startLocationUpdate()
    {
        boolean returnValue = false;


        if(!(GPSInfo.getDefaultGPSMode() == GPSInfo.GPS_MODE_NONE))
        {
            try
            {
                Criteria criteria = new Criteria();
                criteria.setCostAllowed(false);

                _locationProvider = LocationProvider.getInstance(criteria);

                if(_locationProvider != null)
                {
                    /*
                     * Only a single listener can be associated with a provider,
                     * and unsetting it involves the same call but with null.
                     * Therefore, there is no need to cache the listener
                     * instance request an update every second.
                     */
                    _locationProvider.setLocationListener(new LocationListenerImpl(), _interval, -1, -1);
                    returnValue = true;
                }
                else
                {
                    invokeLater(new Runnable()
                    {
                        public void run()
                        {
                            Dialog.alert("Failed to obtain a location provider, exiting...");
                            System.exit(0);
                        }
                    });
                }

            }
            catch(final LocationException le)
            {
                invokeLater(new Runnable()
                {
                    public void run()
                    {
                        Dialog.alert("Failed to instantiate LocationProvider object, exiting..." + le.toString());
                        System.exit(0);
                    }
                });
            }
        }
        else
        {
            invokeLater(new Runnable()
            {
                public void run()
                {
                    Dialog.alert("GPS is not supported on this device, exiting...");
                    System.exit(0);
                }
            });
        }

        return true;
    }

    /**
     * Implementation of the LocationListener interface. Listens for updates to
     * the device location and displays the results.
     */
    private class LocationListenerImpl implements LocationListener
    {

        /**
         * @see javax.microedition.location.LocationListener#locationUpdated(LocationProvider,Location)
         */
        public void locationUpdated(LocationProvider provider, Location location)
        {
            if(location.isValid())
            {
                _longitude = location.getQualifiedCoordinates().getLongitude();
                _latitude = location.getQualifiedCoordinates().getLatitude();

               _mapview.setZoom(Integer.parseInt("0.1"));
                try
                {
                    int latitude = (int) (100000 * _latitude);
                    int longitude = (int) (100000 * _longitude);

                    if (latitude > 9000000 || latitude < -9000000 || longitude >= 18000000 || longitude < -18000000)
                    {
                        throw new IllegalArgumentException ();
                    }

                    _mapview.setLatitude(latitude);
                    _mapview.setLongitude(longitude);

                    // Invoke BlackBerry Maps application with provided MapView object.
                    Invoke.invokeApplication(Invoke.APP_TYPE_MAPS, new MapsArguments(_mapview));
                }
                catch(RuntimeException re)
                {
                    // An exception is thrown when any of the following occur :
                    // Latitude is invalid : Valid range: [-90, 90]
                    // Longitude is invalid : Valid range: [-180, 180)
                    // Minus sign between 2 numbers.
                    Dialog.alert("Temporary Unavailable Service");
                }
            }
        }


        /**
         * @see javax.microedition.location.LocationListener#providerStateChanged(LocationProvider, int)
         */
        public void providerStateChanged(LocationProvider provider, int newState)
        {
            if(newState == LocationProvider.TEMPORARILY_UNAVAILABLE)
            {
                provider.reset();
            }
            _screen.setState(newState);
        }
    }

        /**
     * The main screen to display the current GPS information
     */
    private final class GPSDemoScreen extends MainScreen
    {
        TextField _statusTextField;

        /**
         * Create a new GPSDemoScreen object
         */
        GPSDemoScreen()
        {   
            // Initialize UI
            _statusTextField = new TextField(Field.NON_FOCUSABLE);
        }

        /**
         * Display the state of the GPS service
         * 
         * @param newState The state to display
         */
        public void setState(final int newState)
        {
            UiApplication.getUiApplication().invokeLater(new Runnable()
            {
                /**
                 * @see java.lang.Runnable#run()
                 */
                public void run()
                {
                    switch(newState)
                    {
                        case LocationProvider.AVAILABLE:
                            _statusTextField.setText("Available");
                            break;
                        case LocationProvider.OUT_OF_SERVICE:
                            _statusTextField.setText("Out of Service");
                            break;
                        case LocationProvider.TEMPORARILY_UNAVAILABLE:
                            _statusTextField.setText("Temporarily Unavailable");
                            break;
                    }
                }
            });
        }


        /**
         * @see net.rim.device.api.ui.Screen#close()
         */
        public void close()
        {
            if(_locationProvider != null)
            {
                _locationProvider.reset();
                _locationProvider.setLocationListener(null, -1, -1, -1);
            }

            super.close();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我在事件线程之外使用Dialog.alert()时看到了这个错误。查看代码,我看到LocationListenerImpl.locationUpdated假定它在事件线程上运行。如果不是,UI更新代码将抛出异常,然后您的异常处理程序将尝试显示一个对话框,这也将失败。