【发布时间】:2015-11-04 11:42:10
【问题描述】:
首先,我的程序编译并启动。但是,一旦您在使用 GestureDetector 的 LinearLayout 上进行选项卡,它就会引发 NullPointerException。
我有一个 Activity,其中嵌套了一个名为“OrderFragment”的 Fragment。
public class OrderFragment extends Fragment {
View view;
Activity activty;
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.orderfragment, container, false);
return view;
}
@Override
public void onResume(){
super.onResume();
DragList orderContainer = (DragList) view.findViewById(R.id.orderContainer);
for (int i=0;i<5;++i) {
final TextView tv = new TextView(getActivity().getApplicationContext());
tv.setTextColor(0x888888);
tv.setText("wtf");
orderContainer.addView(tv,new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
}
}
}
在这里,我想以编程方式将一些 TextView 添加到 DragView 中。这个 DragView 存在于 orderfragment.xml 中
在 DragView 类中,我尝试创建一些 TextView,用户可以通过这种方式进行拖动和交换。但我很难让拖动工作。请注意,onTouchEvent 函数是从安卓官方网站here逐字复制而来的
public class DragList extends LinearLayout {
public DragList(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public DragList(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private void init(Context context) {
//do stuff that was in your original constructor...
}
// The active pointer is the one currently moving our object.
private int INVALID_POINTER_ID = -1;
private int mActivePointerId = INVALID_POINTER_ID;
private float mLastTouchX;
private float mLastTouchY;
private GestureDetector mScaleDetector;
public DragList(Context context) {
super(context);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
// Let the ScaleGestureDetector inspect all events.
mScaleDetector.onTouchEvent(ev);
final int action = MotionEventCompat.getActionMasked(ev);
switch (action) {
case MotionEvent.ACTION_DOWN: {
final int pointerIndex = MotionEventCompat.getActionIndex(ev);
final float x = MotionEventCompat.getX(ev, pointerIndex);
final float y = MotionEventCompat.getY(ev, pointerIndex);
// Remember where we started (for dragging)
mLastTouchX = x;
mLastTouchY = y;
// Save the ID of this pointer (for dragging)
mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
break;
}
case MotionEvent.ACTION_MOVE: {
// Find the index of the active pointer and fetch its position
final int pointerIndex =
MotionEventCompat.findPointerIndex(ev, mActivePointerId);
final float x = MotionEventCompat.getX(ev, pointerIndex);
final float y = MotionEventCompat.getY(ev, pointerIndex);
// Calculate the distance moved
final float dx = x - mLastTouchX;
final float dy = y - mLastTouchY;
float mPosX = dx;
float mPosY = dy;
// Remember this touch position for the next move event
mLastTouchX = x;
mLastTouchY = y;
break;
}
case MotionEvent.ACTION_UP: {
mActivePointerId = INVALID_POINTER_ID;
break;
}
case MotionEvent.ACTION_CANCEL: {
mActivePointerId = INVALID_POINTER_ID;
break;
}
case MotionEvent.ACTION_POINTER_UP: {
final int pointerIndex = MotionEventCompat.getActionIndex(ev);
final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
if (pointerId == mActivePointerId) {
// This was our active pointer going up. Choose a new
// active pointer and adjust accordingly.
final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
mLastTouchX = MotionEventCompat.getX(ev, newPointerIndex);
mLastTouchY = MotionEventCompat.getY(ev, newPointerIndex);
mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
}
break;
}
}
return true;
}
}
错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.view.GestureDetector.onTouchEvent(android.view.MotionEvent)' on a null object reference
at com.example.jacobusconradi.versuchstestappv20.DragList.onTouchEvent(DragList.java:46)
而且我还可以确认,DragList 在视觉上是空的,或者不存在,因为我只看到给容器的白色背景色。当我使用调试模式时,它不会抛出任何东西,当我到达以编程方式添加的 TextViews 时。
感谢您的帮助和欢呼
【问题讨论】:
-
我看不到“mScaleDetector”的初始化,因此抛出 Nullpointert..请初始化该变量
-
你在哪里实例化 mScaleDetector?
-
@Satya 我在 onTouchEvent(motionEvent ev) 行上方做了 5 行,在 DragList 的 smalles 构造函数上方
-
@Nanoc 我在 onTouchEvent(motionEvent ev) 行上方做了 5 行,在 DragList 的 smalles 构造函数上方
-
这是一个定义,不是实例化。
标签: android