我正在尝试创建一个示例应用程序,在两个位置之间绘制路线(如超级应用程序)我有一个切换开关在应用程序中打开或关闭位置。应用程序工作正常并绘制路线但是当我只需启动应用程序并将位置开关切换为打开,然后将其切换为关闭应用程序崩溃,在handler.removeCallbacks(drawPathRunnable);
显示nullpointerexception我试图找出错误但我失败了。这是我的代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
location_switch = (MaterialAnimatedSwitch)findViewById(R.id.location_switch);
location_switch.setOnCheckedChangeListener(new MaterialAnimatedSwitch.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(boolean isOnline) {
if(isOnline)
{
startLocationUpdates();
displayLocation();
Snackbar.make(mapFragment.getView(),"You are online", Snackbar.LENGTH_SHORT)
.show();
}
else
{
stopLocationUpdates();
mCurrent.remove();
mMap.clear();
handler.removeCallbacks(drawPathRunnable);
Snackbar.make(mapFragment.getView(),"You are offline", Snackbar.LENGTH_SHORT)
.show();
}
}
});
我已在代码private Handler handler;
请解释错误的答案和原因。
答案 0 :(得分:0)
您应该初始化handler
对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
handler =new Handler();
// Obtain the ......