如何修复 handler.removeCallbacks 导致的应用程序崩溃的错误?
How to fix error at handler.removeCallbacks resulting app crash?
我正在尝试创建一个示例应用程序来绘制两个位置之间的路线(例如优步应用程序)我有一个切换开关可以在 app.The 应用程序中打开或关闭该位置工作正常并绘制路线但是当我刚刚启动应用程序并将位置开关切换到打开然后将其切换到关闭应用程序崩溃时在 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;
请说明答案及错误原因。
您应该初始化您的 handler
对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
handler =new Handler();
// Obtain the ......
我正在尝试创建一个示例应用程序来绘制两个位置之间的路线(例如优步应用程序)我有一个切换开关可以在 app.The 应用程序中打开或关闭该位置工作正常并绘制路线但是当我刚刚启动应用程序并将位置开关切换到打开然后将其切换到关闭应用程序崩溃时在 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;
请说明答案及错误原因。
您应该初始化您的 handler
对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
handler =new Handler();
// Obtain the ......