请在登录按钮后出现等待屏幕

时间:2012-08-09 07:17:23

标签: blackberry popup progress pleasewait

我正在尝试在我的BlackBerry应用程序中实现“等待屏幕”。用户单击“登录”时将显示该屏幕,并且在成功登录后该屏幕将消失。我在“登录”监听器中调用屏幕,之后我调用了一个从web服务器获取数据的方法。获取数据并显示新屏幕时,“等待屏幕”应消失。但是,在点击登录时,我得到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
} 

3 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:2)

与欧根说的一样,你应该在后台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。例如,您有几个直接使用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() );
            }
        } );

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

答案 2 :(得分: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);
    }
}
}
相关问题