我正在尝试将Horizontal Scroll View
发送到某些地方。我尝试使用自定义HorizontalScrollView,使用侦听器“返回”它的位置。然后,在包含它的片段中,它应该检查触摸事件,并且在触摸结束时它应该检查该位置并相应地滚动视图。
以下是代码:
public class CustomHorizontalScrollView extends HorizontalScrollView{
private OnPositionChangeListener listener;
public CustomHorizontalScrollView(Context context) {
super(context);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt){
listener.onPosChange(l);
super.onScrollChanged(l, t, oldl, oldt);
}
public interface OnPositionChangeListener {
public void onPosChange(int l);
}
}
public class CustomFragment extends Fragment implements ArgonHorizontalScrollView.OnPositionChangeListener{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
hScV = new CustomHorizontalScrollView(getActivity());
hScV.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
if(pos>480)
hScV.scrollTo(X, 0);
return true;
}
return false;
}
});
}
@Override
public void onPosChange(int l) {
pos=l;
}
这应该是必不可少的代码。
当我触摸屏幕时出现错误,我不知道为什么。这是日志(很长一段):
java.lang.NullPointerException
03-16 11:48:01.135: E/AndroidRuntime(1118): at com.ex.musicplayer.CustomHorizontalScrollView.onScrollChanged(CustomHorizontalScrollView.java:18)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.View.scrollTo(View.java:10590)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.widget.HorizontalScrollView.onOverScrolled(HorizontalScrollView.java:751)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.View.overScrollBy(View.java:17501)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.widget.HorizontalScrollView.onTouchEvent(HorizontalScrollView.java:605)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.View.dispatchTouchEvent(View.java:7690)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2210)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1945)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
03-16 11:48:01.135: E/AndroidRuntime(1118): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2066)
03-16 11:48:01.135: E/AndroidRuntime(1118): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1515)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.app.Activity.dispatchTouchEvent(Activity.java:2457)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.dispatchTouchEvent(ActionBarActivityDelegateICS.java:260)
03-16 11:48:01.135: E/AndroidRuntime(1118): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2014)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.View.dispatchPointerEvent(View.java:7870)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3919)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3808)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3406)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3456)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3425)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3510)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3433)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3567)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3406)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3456)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3425)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3433)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3406)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5520)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5500)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5471)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5594)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:182)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
03-16 11:48:01.135: E/AndroidRuntime(1118): at android.view.InputEventRe
03-16 11:48:09.325: I/Process(1118): Sending signal. PID: 1118 SIG: 9
答案 0 :(得分:0)
您没有初始化listener
,但是您尝试在此处使用它:
listener.onPosChange(l);
==> NPE