我要做的是在onResume中获取getIntent();
。所有事情都只在MainActivity中发生
这是我的代码,但它无法正常工作
@Override
protected void onResume() {
Thread timer = new Thread() {
public void run() {
try {
sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent intent = getIntent();
String restaurant_name = intent.getStringExtra("restaurant_name");
Toast.makeText(MainActivity.this, restaurant_name, Toast.LENGTH_LONG).show();
if (restaurant_name != null) {
if (restaurant_name.equals("Romys")) {
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(26.89209, 75.82759), 15.0f));
mMap.addMarker(new MarkerOptions()
.position(new LatLng(26.89553, 75.82842))
.title("ROMYS"))
.showInfoWindow();
}
} else {
Toast.makeText(MainActivity.this, "It was not", Toast.LENGTH_LONG).show();
}
}
}
};
timer.start();
super.onResume();
}
我尝试了onResume
onNewInent
,但应用程序崩溃了。
答案 0 :(得分:0)
不确定是否全部,但您正在尝试在不是主UI线程的线程上执行Toast
。所以,尝试更换你的祝酒词:
Toast.makeText(MainActivity.this, restaurant_name, Toast.LENGTH_LONG).show();
使用代码在主UI线程上运行它:
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, restaurant_name, Toast.LENGTH_LONG).show();
}
});