0

my application always crash whenever it reach Toast(parent) part. i tried emptying the entire run() and there were no problem.

this codes work fine in emulator but not on device.

please ignore the Toast if you would like to, my main problem is not the Toast but its the codes onwards. they crash on device.

im merely using the Toast to know where the application crashed.

public class LoadingActivity extends Activity {

/** Duration of wait **/
private final int SPLASH_DISPLAY_LENGTH = 2000;

Intent intent;

LoadingActivity parent;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_loading);

    intent = getIntent();
    parent = this;

    ImageView logo = (ImageView) findViewById(R.id.imageView);

    TextView splash = (TextView) findViewById(R.id.splash);
    splash.setText(intent.getStringExtra(Constant.SPLASH_TEXT));

    TranslateAnimation animator = new TranslateAnimation(logo.getX(), logo.getX(), logo.getY(), logo.getY() + 300);
    animator.setDuration(2000);
    animator.setFillAfter(true);
    logo.startAnimation(animator);

    /* New Handler to start the Menu-Activity
     * and close this Splash-Screen after some seconds.*/
    new Handler().postDelayed(new Runnable(){
        @Override
        public void run() {
//error on here
            Toast.makeText(parent, "im still fine", Toast.LENGTH_LONG).show();
            /* Create an Intent that will start the Menu-Activity. */
            Intent nextIntent = new Intent(parent, HomeActivity.class);
            nextIntent.putExtra(Constant.LOGIN_USERNAME, intent.getStringExtra(Constant.LOGIN_USERNAME));
            parent.startActivity(nextIntent);
            Toast.makeText(parent, "passed", Toast.LENGTH_LONG).show();
            LoadingActivity.this.finish();
        }
    }, SPLASH_DISPLAY_LENGTH);
}
}
4

1 回答 1

1

您不能在后台线程中使用 Toast(任何与 UI 相关的元素),因为工作线程不访问 Ui 元素,因此您可以使用Activity.runOnUiThread(Runnable),也可以使用 Activity 上下文来制作 Toast。

Toast.makeText(LoadingActivity.this, "passed", Toast.LENGTH_LONG).show();

如何在处理程序/线程中显示 Toast?

于 2015-04-29T05:22:18.337 回答