我的应用程序一旦到达Toast(父)部分就会崩溃。我试着清空整个run()并且没有问题。
此代码在模拟器中正常工作,但在设备上无效。
如果您愿意,请忽略Toast,我的主要问题不是Toast,而是其代码。他们在设备上崩溃了。
我只是使用Toast知道应用程序崩溃的位置。
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);
}
}
答案 0 :(得分:1)
您不能在后台线程中使用Toast(任何UI相关元素),因为工作线程不能访问Ui元素,因此您可以使用Activity.runOnUiThread(Runnable)
,也可以使用Activity上下文来制作Toast。
Toast.makeText(LoadingActivity.this, "passed", Toast.LENGTH_LONG).show();