我找不到问题所在,我认为我已将内容正确放入项目和clipdata中,
但是我的onDrag总是收到一个空指针异常,甚至无法获得项目数。
这是我的OnDragListener类
public class ScaleImageSize implements View.OnDragListener{
@Override
public boolean onDrag(View arg0, DragEvent arg1) {
// TODO Auto-generated method stub
final int action = arg1.getAction();
Integer tag = (Integer) arg0.getTag();
Log.d("tag",Integer.toString(tag));
int count = arg1.getClipData().getItemCount();
Log.d("count",Integer.toString(count));
ClipData.Item item = arg1.getClipData().getItemAt(0);
String tagString = item.getText().toString();
int tagOfType = Integer.valueOf(tagString);
switch(action){
case DragEvent.ACTION_DRAG_STARTED:
return true;
case DragEvent.ACTION_DRAG_LOCATION:
augImg( tag, (int) arg1.getY());
augImg( tagOfType, (int)arg1.getY());
return true;
case DragEvent.ACTION_DROP:
return true;
}
return false;
}
};
这是我的OnLonClickListener类,应调用函数startdrag。
OnLongClickListener StartScaleImageSize = new OnLongClickListener(){
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
int i = (Integer) v.getTag();
beingLongClick[i] = true;
ClipData.Item item = new ClipData.Item((CharSequence)Integer.toString(i));
String[] clipDescription = {ClipDescription.MIMETYPE_TEXT_PLAIN};
ClipData dragData = new ClipData((CharSequence)Integer.toString(i),
clipDescription,
item);
dragData.addItem(item);
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
v.startDrag(dragData, shadowBuilder, v, 0);
return false;
}
};
和错误消息:
06-24 21:47:21.205: E/AndroidRuntime(4123): FATAL EXCEPTION: main
06-24 21:47:21.205: E/AndroidRuntime(4123): java.lang.NullPointerException
06-24 21:47:21.205: E/AndroidRuntime(4123): at traveler.com.TravelerpActivity$TypeMgr$ScaleImageSize.onDrag(TravelerpActivity.java:237)
06-24 21:47:21.205: E/AndroidRuntime(4123): at android.view.View.dispatchDragEvent(View.java:13619)
06-24 21:47:21.205: E/AndroidRuntime(4123): at android.view.ViewGroup.notifyChildOfDrag(ViewGroup.java:1287)
06-24 21:47:21.205: E/AndroidRuntime(4123): at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1131)
06-24 21:47:21.205: E/AndroidRuntime(4123): at android.view.ViewGroup.notifyChildOfDrag(ViewGroup.java:1287)
06-24 21:47:21.205: E/AndroidRuntime(4123): at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1131)
06-24 21:47:21.205: E/AndroidRuntime(4123): at android.view.ViewGroup.notifyChildOfDrag(ViewGroup.java:1287)
06-24 21:47:21.205: E/AndroidRuntime(4123): at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1131)
06-24 21:47:21.205: E/AndroidRuntime(4123): at android.view.ViewGroup.notifyChildOfDrag(ViewGroup.java:1287)
06-24 21:47:21.205: E/AndroidRuntime(4123): at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1131)
06-24 21:47:21.205: E/AndroidRuntime(4123): at android.view.ViewRootImpl.handleDragEvent(ViewRootImpl.java:3471)
06-24 21:47:21.205: E/AndroidRuntime(4123): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2620)
06-24 21:47:21.205: E/AndroidRuntime(4123): at android.os.Handler.dispatchMessage(Handler.java:99)
06-24 21:47:21.205: E/AndroidRuntime(4123): at android.os.Looper.loop(Looper.java:137)
06-24 21:47:21.205: E/AndroidRuntime(4123): at android.app.ActivityThread.main(ActivityThread.java:4441)
06-24 21:47:21.205: E/AndroidRuntime(4123): at java.lang.reflect.Method.invokeNative(Native Method)
06-24 21:47:21.205: E/AndroidRuntime(4123): at java.lang.reflect.Method.invoke(Method.java:511)
06-24 21:47:21.205: E/AndroidRuntime(4123): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-24 21:47:21.205: E/AndroidRuntime(4123): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-24 21:47:21.205: E/AndroidRuntime(4123): at dalvik.system.NativeStart.main(Native Method)
请给我任何建议!
最佳答案
尝试以这种方式创建dragData:
ClipData dragData = ClipData.newPlainText("simple text", toString(i));
关于android - 拖动但空的clipdata,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11178079/