0

我正在尝试在我的 BlackBerry 应用程序中实现“等待屏幕”。当用户单击“登录”时会出现该屏幕,并在成功登录后消失。我在“登录”侦听器中调用屏幕,然后调用方法从网络服务获取数据。获取数据并显示新屏幕后,“等待屏幕”应该会消失。但是,单击登录Uncaught - RuntimeException后,我会在新屏幕上显示“等待屏幕”。有人可以帮我吗?

public class MessageScreen extends PopupScreen
{
    private String message;

    public MessageScreen (String message)
    {
        super( new HorizontalFieldManager(), Field.NON_FOCUSABLE);
        this.message = message;
        final BitmapField logo = new BitmapField(Bitmap.getBitmapResource( "cycle.gif"));
        logo.setSpace( 5, 5 );
        add(logo);

        RichTextField rtf = new RichTextField(message, Field.FIELD_VCENTER | Field.NON_FOCUSABLE | Field.FIELD_HCENTER);
        rtf.setEditable( false );

        add(rtf);
    }
}

我在“登录”单击事件中调用它 - 按钮侦听器。

public void fieldChanged(Field field, int context)
{
    // Push appropriate screen depending on which button was clicked
    String uname = username.getText();
    String pwd = passwd.getText();
    if (uname.length() == 0 || pwd.length()==0) {
        Dialog.alert("One of the textfield is empty!");
    } else {
        C0NNECTION_EXTENSION=checkInternetConnection();
        if(C0NNECTION_EXTENSION==null)
        {
            Dialog.alert("Check internet connection and try again");
        }
        else
        {
            UiApplication.getUiApplication().invokeLater( new Runnable()
            {
                public void run ()
                {
                    UiApplication.getUiApplication().pushScreen( new MessageScreen("Signing in...") );
                }
            } );
            doLogin(uname, pwd);
        }
    }
}

private String doLogin(String user_id, String password)
{
    String URL ="";
    String METHOD_NAME = "ValidateCredentials";
    String NAMESPACE = "http://tempuri.org/";
    String SOAP_ACTION = NAMESPACE+METHOD_NAME;
    SoapObject resultRequestSOAP = null;
    HttpConnection httpConn = null;
    HttpTransport httpt;
    SoapPrimitive response = null;
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    request.addProperty("username", user_id);
    request.addProperty("password", password);
    System.out.println("The request is=======" + request.toString());
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);
    httpt = new HttpTransport(URL+C0NNECTION_EXTENSION);
    httpt.debug = true;
    try
    {
        httpt.call(SOAP_ACTION, envelope);
        response = (SoapPrimitive) envelope.getResponse();
        String result =  response.toString();
        resultRequestSOAP = (SoapObject) envelope.bodyIn;
        String[] listResult = split(result, sep);
        strResult = listResult[0].toString();
        strsessionFirstName = listResult[1].toString();
        strsessionLastName = listResult[2].toString();
        strsessionPictureUrl = MAINURL + listResult[3].substring(2);
        strsessionStatusId = listResult[4].toString();
        strsessionStatusMessage = listResult[5].toString();
        strsessionLastUpdateTst = listResult[6].toString();

        if(strResult.equals("credentialaccepted"))
        {
            if(checkBox1.getChecked() == true)
            {
                persistentHashtable.put("username", user_id);
                persistentHashtable.put("password", password);
            }
            Bitmap bitmap = getLiveImage(strsessionPictureUrl, 140, 140);
            StatusActivity nextScreen = new StatusActivity();
            nextScreen.getUsername(user_id);
            nextScreen.getPassword(password);
            nextScreen.setPictureUrl(bitmap);
            nextScreen.setImage(strsessionPictureUrl);
            nextScreen.setFirstName(strsessionFirstName, strsessionLastName, strsessionLastUpdateTst, strsessionStatusMessage);
            UiApplication.getUiApplication().pushScreen(nextScreen);
            UiApplication.getUiApplication().invokeLater( new Runnable()
            {
                public void run ()
                {
                    UiApplication.getUiApplication().pushScreen( UiApplication.getUiApplication().getActiveScreen() );
                }
            } );
        }
        if(strResult.equals("credentialdenied"))
        {
            Dialog.alert("Invalid login details.");
            UiApplication.getUiApplication().pushScreen(new LoginTestScreen() );
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        System.out.println("The exception is IO==" + e.getMessage());
    } catch (XmlPullParserException e) {
        // TODO Auto-generated catch block
        System.out.println("The exception xml parser example==="
        + e.getMessage());
    }

    System.out.println( resultRequestSOAP);
    //UiApplication.getUiApplication().pushScreen( UiApplication.getUiApplication().getActiveScreen() );
    return response + "";

    //UiApplication.getUiApplication().pushScreen(new InfoScreen());
    //Open a new Screen
} 
4

3 回答 3

2

到目前为止,我只看到一个问题 - UI 线程中的网络。请将您所有的网络操作放到另一个Thread.run().

您还可以通过以下方式获得更详细的错误描述:1) 导航到主屏幕 2) 按住 alt 按钮并按键盘上的 LGLG 3) 探索显示特定错误的事件日志

于 2012-08-09T08:55:32.453 回答
2

就像 Eugen 说的,你应该doLogin()在后台运行Thread

final String uname = username.getText();
final String pwd = passwd.getText();

Thread backgroundWorker = new Thread(new Runnable() {
    public void run() {
        doLogin(uname, pwd);
    }
});

backgroundWorker.start();

如果这样做,您将需要使用UiApplication.invokeLater()(或其他类似技术)来显示您的屏幕(返回主/UI 线程)。您不能doLogin()完全按照原来的方式保留该方法,因为它会调用更改 UI。例如,您有几个直接调用 use pushScreen(),不应该(直接)从后台调用。

这不好(从后台):

        UiApplication.getUiApplication().pushScreen(nextScreen);

但是,这是:

        UiApplication.getUiApplication().invokeLater( new Runnable()
        {
            public void run ()
            {
                UiApplication.getUiApplication().pushScreen(nextScreen);
            }
        } );

但是,同样,这段代码应该做什么?:

        UiApplication.getUiApplication().pushScreen(nextScreen);
        UiApplication.getUiApplication().invokeLater( new Runnable()
        {
            public void run ()
            {
                UiApplication.getUiApplication().pushScreen( UiApplication.getUiApplication().getActiveScreen() );
            }
        } );

这对我来说没有意义。你想用这些代码行做什么?

于 2012-08-09T09:23:50.737 回答
1

试试这个 -

public void fieldChanged(Field field, int context)
{
// Push appropriate screen depending on which button was clicked
String uname = username.getText();
String pwd = passwd.getText();
if (uname.length() == 0 || pwd.length()==0) {
    Dialog.alert("One of the textfield is empty!");
} else {
    C0NNECTION_EXTENSION=checkInternetConnection();
    if(C0NNECTION_EXTENSION==null)
    {
        Dialog.alert("Check internet connection and try again");
    }
    else
    {

    Dialog busyDialog = new Dialog("Signing in...", null, null, 0, Bitmap.getPredefinedBitmap(Bitmap.HOURGLASS));
    busyDialog.setEscapeEnabled(false);
    synchronized (Application.getEventLock()) {
    busyDialog.show();
    }
    doLogin(uname, pwd);
    }
}
}
于 2012-08-09T09:23:30.283 回答