我正在尝试从活动到自定义视图设置接口侦听器,但它正在抛出NullPointerException。当我触摸CustomRelativeLayout时,它将通过dispatchTouchEvent
回调方法处理,但它会抛出异常
请帮帮我...
CustomRelativeLayout.class
public class CustomRelativeLayout extends RelativeLayout {
private static final String TAG = "CustomRelative";
private MapsActivity.MapTouchable mapTouchable;
public CustomRelativeLayout(Context context) {
super(context);
}
public CustomRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.e(TAG, "dispatchTouchEvent: Action Down");
mapTouchable.onMapTouchEvent(true);
break;
case MotionEvent.ACTION_UP:
Log.e(TAG, "dispatchTouchEvent: Action Up");
mapTouchable.onMapTouchEvent(false);
break;
case MotionEvent.ACTION_MOVE:
Log.e(TAG, "dispatchTouchEvent: Action Move");
mapTouchable.onMapTouchEvent(true);
break;
}
return super.dispatchTouchEvent(event);
}
public void setOnMapTouchListner(MapsActivity.MapTouchable mapsActivity) {
mapTouchable = mapsActivity;
}
}
这里是活动类
public class MapsActivity extends FragmentActivity {
private static final String TAG = "MapsActivity";
private CustomRelativeLayout rl_screenview;
public interface MapTouchable {
void onMapTouchEvent(boolean isTouch);
}
MapTouchable mapTouchable = new MapTouchable() {
@Override
public void onMapTouchEvent(boolean isTouch) {
Log.e(TAG, "onMapTouchEvent() called with: " + "isTouch = [" + isTouch + "]");
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
Log.e(TAG, "onCreate: ");
rl_screenview = (CustomRelativeLayout)findViewById(R.id.rl_screenview);
// here i am setting listner to customrelativelayout
rl_screenview.setOnMapTouchListner(mapTouchable);
}
}
帮助将被宣布......