简体   繁体   English

回调内的意图-android

[英]Intent inside callback - android

Im using Parse for my back end and just created a simple login app where I am trying to add an intent to go to the home screen from my login screen.Here's my code: 我在后端使用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();
                    }
                }});
}
}

So according to the code my app should go to the "Home" page but it crashes and stops.Any way around this? 因此,根据代码,我的应用程序应该转到“主页”页面,但它崩溃并停止。

loginInBackground suggests that you are performing operations in a thread different from the UI Thread. loginInBackground建议您在与UI线程不同的线程中执行操作。 But only the UI Thread can show a toast (or modify UI elements). 但是,只有UI线程可以显示敬酒(或修改UI元素)。

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

You can use an Handler or runOnUiThread to overcame your issue 您可以使用HandlerrunOnUiThread来解决您的问题

AS @Blackbelt has suggested you need to run Toast in another thread. AS @Blackbelt建议您需要在另一个线程中运行Toast。 Use the Following Code 使用以下代码

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

And this code where you wanna toast 而这个代码你想敬酒

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

Alternatively you can use *runonUiThread* : 或者,您可以使用*runonUiThread*

                runOnUiThread(new Runnable() {

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM