2013-08-06 8 views
0

지도에서 사용자로부터의 상호 작용이있을 때이를 감지하도록 TouchableWrapper 클래스를 설정했습니다. 코드는 우선 override 메소드를 통해 활동에 알림을 보내도록 설정되었습니다. 그러나 내 맵은 Fragment에 있으며이 오버라이드를 받고 싶습니다. 지금까지 널 포인터 예외가 계속 발생합니다.인터페이스를 사용하는 비 활동 클래스 간의 통신

TouchableWrapper 클래스 다음

public class TouchableWrapper extends FrameLayout { 

private long lastTouched = 0; 
private static final long SCROLL_TIME = 200L; // 200 Milliseconds, but you 
               // can adjust that to your 
               // liking 
private UpdateMapAfterUserInterection updateMapAfterUserInterection; 

// public TouchableWrapper(Context context) { 
// super(context); 
// // Force the host activity to implement the UpdateMapAfterUserInterection 
// Interface 
// // try { 
// // updateMapAfterUserInterection = (UpdateMapAfterUserInterection) 
// context; 
// // } catch (ClassCastException e) { 
// // throw new ClassCastException(context.toString() + 
// " must implement UpdateMapAfterUserInterection"); 
// // } 
// } 

public TouchableWrapper(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

public TouchableWrapper(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
} 

public TouchableWrapper(Context context) { 
    super(context); 
    setUpdateMapAfterUserInterection(updateMapAfterUserInterection); 
} 

@Override 
public boolean dispatchTouchEvent(MotionEvent ev) { 
    switch (ev.getAction()) { 
    case MotionEvent.ACTION_DOWN: 
     lastTouched = SystemClock.uptimeMillis(); 
     break; 
    case MotionEvent.ACTION_UP: 
     final long now = SystemClock.uptimeMillis(); 
     if (now - lastTouched > SCROLL_TIME) { 
      // Update the map 
      updateMapAfterUserInterection.onUpdateMapAfterUserInterection(); 
     } 
     break; 
    } 
    return super.dispatchTouchEvent(ev); 
} 

// Map Activity must implement this interface 
public interface UpdateMapAfterUserInterection { 
    public void onUpdateMapAfterUserInterection(); 
} 

public void setUpdateMapAfterUserInterection(UpdateMapAfterUserInterection mUpdateMapAfterUserInterection) { 
    this.updateMapAfterUserInterection = mUpdateMapAfterUserInterection; 
} 
} 

내가 내지도

TouchableWrapper tW = new TouchableWrapper(getActivity()); 
     tW.setUpdateMapAfterUserInterection(new UpdateMapAfterUserInterection() { 

      @Override 
      public void onUpdateMapAfterUserInterection() { 
       Log.d("MAP", "TOUCHED? YEP"); 
      } 
     }); 

가 어떻게이 내가 그것을 원하는 방식으로 작동시킬 수 있습니다 포함 내 조각 클래스에서 사용하려 한 방법이다.

답변

관련 문제