0

我在后端使用 Parse,并且刚刚创建了一个简单的登录应用程序,我试图在其中添加从登录屏幕转到主屏幕的意图。这是我的代码:

public void loginClicked(View v)
{
    // Retrieve the text entered from the EditText
    usernametxt = username.getText().toString();
    passwordtxt = password.getText().toString();

    // Send data to Parse.com for verification
    ParseUser.logInInBackground(usernametxt,
            passwordtxt,
            new LogInCallback(){

                @Override
                public void done(ParseUser user, ParseException e) {
                    // TODO Auto-generated method stub
                    if (user != null) {
                        // If user exist and authenticated, send user to Welcome.class
                     Intent intent2 = new Intent(MainActivity.this,Home.class);
                 startActivity(intent2);
                        Toast.makeText(getApplicationContext(),
                                "Successfully Logged in",
                                Toast.LENGTH_LONG).show();
                        finish();
                    } else {
                        Toast.makeText(
                                getApplicationContext(),
                                "No such user exist, please signup",
                                Toast.LENGTH_LONG).show();
                    }
                }});
}
}

因此,根据代码,我的应用程序应该转到“主页”页面,但它会崩溃并停止。有什么办法解决这个问题吗?

4

2 回答 2

0

AS @Blackbelt 建议您需要在另一个线程中运行 Toast。使用以下代码

    Handler mHandler = new Handler();//Best Initiated inside the onCreate method of your  activity

这段代码是你想敬酒的地方

mHandler.post(new Runnable() {
                    public void run() {
                        Toast.makeText(getApplicationContext(),
                                "Successfully Logged in",
                                Toast.LENGTH_LONG).show();
                    }
                });

或者,您可以使用*runonUiThread*

                runOnUiThread(new Runnable() {

                    Toast.makeText(getApplicationContext(),
                                "Successfully Logged in",
                                Toast.LENGTH_LONG).show();
                });
于 2013-09-21T21:39:14.213 回答
0

loginInBackground 建议您在不同于 UI 线程的线程中执行操作。但只有 UI Thread 可以显示 toast(或修改 UI 元素)。

 Toast.makeText(getApplicationContext(),
                                "Successfully Logged in",
                                Toast.LENGTH_LONG).show();

您可以使用HandlerorrunOnUiThread来解决您的问题

于 2013-09-21T21:27:19.743 回答