myReverseGeocode 未启动。

发布于 2024-12-03 04:36:06 字数 2520 浏览 1 评论 0原文

在下面的代码中,当我调用 myReverseGeocode address = new myReverseGeocode(latitude, longitude); 时, gpsData.append(address.temp);总是显示“空”

public HelloBlackBerryScreen() {
        super( MainScreen.VERTICAL_SCROLL | MainScreen.VERTICAL_SCROLLBAR );
        setTitle( "Get Address" );

        ButtonField buttonField_1 = new ButtonField( "Get GPS", ButtonField.CONSUME_CLICK | ButtonField.FIELD_RIGHT );
        add( buttonField_1 );
        buttonField_1.setChangeListener( new FieldChangeListener() {
            public void fieldChanged( Field arg0, int arg1 ) {
                Criteria c = new Criteria();                      

                try {
                    LocationProvider lp = LocationProvider.getInstance(c);
                    if( lp!=null )
                    {
                        lp.setLocationListener(new LocationListenerImpl(), 3, 1, 1);
                        myReverseGeocode address = new myReverseGeocode(latitude, longitude);

                        gpsData.append(address.temp);
                        myloc = gpsData.toString();
                    }



                } catch (LocationException le) {                        
                    ;
                }

myReverseGeocode.java


package mypackage;

import javax.microedition.location.AddressInfo;
import javax.microedition.location.Landmark;

import net.rim.device.api.lbs.Locator;
import net.rim.device.api.lbs.LocatorException;

public class myReverseGeocode {
    private Thread reverseGeocode;
    public AddressInfo addrInfo = null;
    String temp;

    public myReverseGeocode(final double lt, final double ln)
    {
         Runnable thread = new Runnable()
            {
                public void run()
                {

                    int latitude  = (int)(lt * 100000);
                    int longitude = (int)(ln * 100000);
                    try
                    {
                        Landmark[] results = Locator.reverseGeocode(latitude, longitude, Locator.ADDRESS );

                        if ( results != null && results.length > 0 )
                            temp = "inside if";
                        else
                            temp = "in else";
                    }
                    catch ( LocatorException lex )
                    {
                    }
                }
            };
        reverseGeocode = new Thread(thread);
        reverseGeocode.setPriority(Thread.MIN_PRIORITY);
        reverseGeocode.start();
    }

}

In below code when i call myReverseGeocode address = new myReverseGeocode(latitude, longitude); , gpsData.append(address.temp); always show "null"

public HelloBlackBerryScreen() {
        super( MainScreen.VERTICAL_SCROLL | MainScreen.VERTICAL_SCROLLBAR );
        setTitle( "Get Address" );

        ButtonField buttonField_1 = new ButtonField( "Get GPS", ButtonField.CONSUME_CLICK | ButtonField.FIELD_RIGHT );
        add( buttonField_1 );
        buttonField_1.setChangeListener( new FieldChangeListener() {
            public void fieldChanged( Field arg0, int arg1 ) {
                Criteria c = new Criteria();                      

                try {
                    LocationProvider lp = LocationProvider.getInstance(c);
                    if( lp!=null )
                    {
                        lp.setLocationListener(new LocationListenerImpl(), 3, 1, 1);
                        myReverseGeocode address = new myReverseGeocode(latitude, longitude);

                        gpsData.append(address.temp);
                        myloc = gpsData.toString();
                    }



                } catch (LocationException le) {                        
                    ;
                }

myReverseGeocode.java


package mypackage;

import javax.microedition.location.AddressInfo;
import javax.microedition.location.Landmark;

import net.rim.device.api.lbs.Locator;
import net.rim.device.api.lbs.LocatorException;

public class myReverseGeocode {
    private Thread reverseGeocode;
    public AddressInfo addrInfo = null;
    String temp;

    public myReverseGeocode(final double lt, final double ln)
    {
         Runnable thread = new Runnable()
            {
                public void run()
                {

                    int latitude  = (int)(lt * 100000);
                    int longitude = (int)(ln * 100000);
                    try
                    {
                        Landmark[] results = Locator.reverseGeocode(latitude, longitude, Locator.ADDRESS );

                        if ( results != null && results.length > 0 )
                            temp = "inside if";
                        else
                            temp = "in else";
                    }
                    catch ( LocatorException lex )
                    {
                    }
                }
            };
        reverseGeocode = new Thread(thread);
        reverseGeocode.setPriority(Thread.MIN_PRIORITY);
        reverseGeocode.start();
    }

}

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

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

发布评论

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

评论(1

木格 2024-12-10 04:36:06

您总是将 temp 设为 null,因为您几乎在 Thread 之后立即请求它(在 myReverseGeocode< 的构造函数中启动) /code>) 已启动。您需要重新设计 myReverseGeocode 以在获取实际 temp 值后更新屏幕的 UI。

You always get temp as null because you ask for it almost immediatelly after the Thread (that is started in constructor of the myReverseGeocode) has been started. You need to redesign your myReverseGeocode to udate the UI of the screen after an actual temp value is got.

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