将图像从对话框拖放到 activity 时出现问题
Problem With Dropping an image from dialog to activity
我一直收到 NullPointerException
。我无法将图像从我的 dialogfragment 获取到我的主要 activity。对话框中的项目拖到主 activity。但是当我放下图像时,应用程序崩溃了,因为它没有得到我拖动的图像。那么如何将图像从我的对话框拖放到我的主activity?请帮我解决这个问题。
Main Activity
public class MainActivity extends AppCompatActivity implements View.OnDragListener, View.OnLongClickListener {
private static final String TAG = MainActivity.class.getSimpleName();
private ImageView imageView;
private static final String IMAGE_VIEW_TAG = "ONION";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViews();
implementEvents();
}
private void findViews() {
imageView = findViewById(R.id.image_view);
imageView.setTag(IMAGE_VIEW_TAG);
}
//Implement long click and drag listener
private void implementEvents() {
imageView.setOnLongClickListener(this);
findViewById(R.id.Imgbutton10).setOnDragListener(this);
findViewById(R.id.Imgbutton11).setOnDragListener(this);
findViewById(R.id.Imgbutton12).setOnDragListener(this);
findViewById(R.id.Imgbutton13).setOnDragListener(this);
findViewById(R.id.Imgbutton14).setOnDragListener(this);
}
@Override
public boolean onLongClick(View view) {
ClipData.Item item = new ClipData.Item((CharSequence) view.getTag());
String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
ClipData data = new ClipData(view.getTag().toString(), mimeTypes, item);
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data,shadowBuilder,view,0);
view.setVisibility(View.INVISIBLE);
return true;
}
@Override
public boolean onDrag(View view, DragEvent event) {
int action = event.getAction();
switch (action) {
case DragEvent.ACTION_DRAG_STARTED:
if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
return true;
}
return false;
case DragEvent.ACTION_DRAG_ENTERED:
view.getBackground().setColorFilter(Color.DKGRAY, PorterDuff.Mode.SRC_IN);
view.invalidate();
return true;
case DragEvent.ACTION_DRAG_LOCATION:
// Ignore the event
return true;
case DragEvent.ACTION_DRAG_EXITED:
view.getBackground().clearColorFilter();
view.invalidate();
return true;
case DragEvent.ACTION_DROP:
ClipData.Item item = event.getClipData().getItemAt(0);
String dragData = item.getText().toString();
Toast.makeText(this, "Dragged data is " + dragData, Toast.LENGTH_SHORT).show();
view.getBackground().clearColorFilter();
view.invalidate();
View v = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) v.getParent();
owner.removeView(v);
LinearLayout container = (LinearLayout) view;
container.addView(v);
v.setVisibility(View.VISIBLE);
return true;
case DragEvent.ACTION_DRAG_ENDED:
view.getBackground().clearColorFilter();
view.invalidate();
if (event.getResult())
Toast.makeText(this, "The drop was handled.", Toast.LENGTH_SHORT).show();
else
Toast.makeText(this, "The drop didn't work.", Toast.LENGTH_SHORT).show();
return true;
default:
Log.e("DragDrop Example", "Unknown action type received by OnDragListener.");
break;
}
return false;
}
}
Dialog Fragment
public class VegyFrag extends DialogFragment implements View.OnLongClickListener,View.OnDragListener {
private static final String Tag = "VegyFrag";
private ImageView imageView;
private static final String IMAGE_VIEW_TAG = "ONION";
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View myView = inflater.inflate(R.layout.dialog_add, container, false);
imageView = myView.findViewById(R.id.ic2);
imageView.setTag(IMAGE_VIEW_TAG);
implementEvents();
return myView;
}
private void implementEvents() {
//add or remove any view that you don't want to be dragged
imageView.setOnLongClickListener(this);
((MainActivity)getActivity()).findViewById(R.id.Imgbutton10).setOnDragListener(this);
((MainActivity)getActivity()).findViewById(R.id.Imgbutton11).setOnDragListener(this);
((MainActivity)getActivity()).findViewById(R.id.Imgbutton12).setOnDragListener(this);
((MainActivity)getActivity()).findViewById(R.id.Imgbutton13).setOnDragListener(this);
((MainActivity)getActivity()).findViewById(R.id.Imgbutton14).setOnDragListener(this);
}
@Override
public boolean onLongClick(View view) {
ClipData.Item item = new ClipData.Item((CharSequence) view.getTag());
String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
ClipData data = new ClipData(view.getTag().toString(), mimeTypes, item);
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data,shadowBuilder,view,0);
view.setVisibility(View.INVISIBLE);
getDialog().dismiss();
return true;
}
@Override
public boolean onDrag(View view, DragEvent event) {
int action = event.getAction();
switch (action) {
case DragEvent.ACTION_DRAG_STARTED:
if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
return true;
}
return false;
case DragEvent.ACTION_DRAG_ENTERED:
view.getBackground().setColorFilter(Color.DKGRAY, PorterDuff.Mode.SRC_IN);
view.invalidate();
return true;
case DragEvent.ACTION_DRAG_LOCATION:
return true;
case DragEvent.ACTION_DRAG_EXITED:
view.getBackground().clearColorFilter();
view.invalidate();
return true;
case DragEvent.ACTION_DROP:
ClipData.Item item = event.getClipData().getItemAt(0);
String dragData = item.getText().toString();
view.getBackground().clearColorFilter();
view.invalidate();
View v = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) v.getParent();
owner.removeView(v);
LinearLayout container = (LinearLayout) view;
container.addView(v);
v.setVisibility(View.VISIBLE);
return true;
case DragEvent.ACTION_DRAG_ENDED:
view.getBackground().clearColorFilter();
view.invalidate();
return true;
default:
Log.e("DragDrop Example", "Unknown action type received by OnDragListener.");
break;
}
return false;
}
}
提前致谢
我也找不到使用 DialogFragment 的方法,所以我找到了解决方法。 DialogFragments 的问题是当对话框被关闭时视图被破坏,因此您遇到了 NPE。为了解决这个问题,我在底层片段的视图中添加了一个看起来像对话框的视图(使用 CardView)。当 "dialog" 需要显示时,我将可见性设置为 View.VISIBLE,当它需要隐藏时,我将其设置为 View.GONE。这使您正在拖动的视图与片段保持在同一上下文中,并且在隐藏 "dialog" 时不会被销毁。
我一直收到 NullPointerException
。我无法将图像从我的 dialogfragment 获取到我的主要 activity。对话框中的项目拖到主 activity。但是当我放下图像时,应用程序崩溃了,因为它没有得到我拖动的图像。那么如何将图像从我的对话框拖放到我的主activity?请帮我解决这个问题。
Main Activity
public class MainActivity extends AppCompatActivity implements View.OnDragListener, View.OnLongClickListener {
private static final String TAG = MainActivity.class.getSimpleName();
private ImageView imageView;
private static final String IMAGE_VIEW_TAG = "ONION";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViews();
implementEvents();
}
private void findViews() {
imageView = findViewById(R.id.image_view);
imageView.setTag(IMAGE_VIEW_TAG);
}
//Implement long click and drag listener
private void implementEvents() {
imageView.setOnLongClickListener(this);
findViewById(R.id.Imgbutton10).setOnDragListener(this);
findViewById(R.id.Imgbutton11).setOnDragListener(this);
findViewById(R.id.Imgbutton12).setOnDragListener(this);
findViewById(R.id.Imgbutton13).setOnDragListener(this);
findViewById(R.id.Imgbutton14).setOnDragListener(this);
}
@Override
public boolean onLongClick(View view) {
ClipData.Item item = new ClipData.Item((CharSequence) view.getTag());
String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
ClipData data = new ClipData(view.getTag().toString(), mimeTypes, item);
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data,shadowBuilder,view,0);
view.setVisibility(View.INVISIBLE);
return true;
}
@Override
public boolean onDrag(View view, DragEvent event) {
int action = event.getAction();
switch (action) {
case DragEvent.ACTION_DRAG_STARTED:
if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
return true;
}
return false;
case DragEvent.ACTION_DRAG_ENTERED:
view.getBackground().setColorFilter(Color.DKGRAY, PorterDuff.Mode.SRC_IN);
view.invalidate();
return true;
case DragEvent.ACTION_DRAG_LOCATION:
// Ignore the event
return true;
case DragEvent.ACTION_DRAG_EXITED:
view.getBackground().clearColorFilter();
view.invalidate();
return true;
case DragEvent.ACTION_DROP:
ClipData.Item item = event.getClipData().getItemAt(0);
String dragData = item.getText().toString();
Toast.makeText(this, "Dragged data is " + dragData, Toast.LENGTH_SHORT).show();
view.getBackground().clearColorFilter();
view.invalidate();
View v = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) v.getParent();
owner.removeView(v);
LinearLayout container = (LinearLayout) view;
container.addView(v);
v.setVisibility(View.VISIBLE);
return true;
case DragEvent.ACTION_DRAG_ENDED:
view.getBackground().clearColorFilter();
view.invalidate();
if (event.getResult())
Toast.makeText(this, "The drop was handled.", Toast.LENGTH_SHORT).show();
else
Toast.makeText(this, "The drop didn't work.", Toast.LENGTH_SHORT).show();
return true;
default:
Log.e("DragDrop Example", "Unknown action type received by OnDragListener.");
break;
}
return false;
}
}
Dialog Fragment
public class VegyFrag extends DialogFragment implements View.OnLongClickListener,View.OnDragListener {
private static final String Tag = "VegyFrag";
private ImageView imageView;
private static final String IMAGE_VIEW_TAG = "ONION";
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View myView = inflater.inflate(R.layout.dialog_add, container, false);
imageView = myView.findViewById(R.id.ic2);
imageView.setTag(IMAGE_VIEW_TAG);
implementEvents();
return myView;
}
private void implementEvents() {
//add or remove any view that you don't want to be dragged
imageView.setOnLongClickListener(this);
((MainActivity)getActivity()).findViewById(R.id.Imgbutton10).setOnDragListener(this);
((MainActivity)getActivity()).findViewById(R.id.Imgbutton11).setOnDragListener(this);
((MainActivity)getActivity()).findViewById(R.id.Imgbutton12).setOnDragListener(this);
((MainActivity)getActivity()).findViewById(R.id.Imgbutton13).setOnDragListener(this);
((MainActivity)getActivity()).findViewById(R.id.Imgbutton14).setOnDragListener(this);
}
@Override
public boolean onLongClick(View view) {
ClipData.Item item = new ClipData.Item((CharSequence) view.getTag());
String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
ClipData data = new ClipData(view.getTag().toString(), mimeTypes, item);
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data,shadowBuilder,view,0);
view.setVisibility(View.INVISIBLE);
getDialog().dismiss();
return true;
}
@Override
public boolean onDrag(View view, DragEvent event) {
int action = event.getAction();
switch (action) {
case DragEvent.ACTION_DRAG_STARTED:
if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
return true;
}
return false;
case DragEvent.ACTION_DRAG_ENTERED:
view.getBackground().setColorFilter(Color.DKGRAY, PorterDuff.Mode.SRC_IN);
view.invalidate();
return true;
case DragEvent.ACTION_DRAG_LOCATION:
return true;
case DragEvent.ACTION_DRAG_EXITED:
view.getBackground().clearColorFilter();
view.invalidate();
return true;
case DragEvent.ACTION_DROP:
ClipData.Item item = event.getClipData().getItemAt(0);
String dragData = item.getText().toString();
view.getBackground().clearColorFilter();
view.invalidate();
View v = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) v.getParent();
owner.removeView(v);
LinearLayout container = (LinearLayout) view;
container.addView(v);
v.setVisibility(View.VISIBLE);
return true;
case DragEvent.ACTION_DRAG_ENDED:
view.getBackground().clearColorFilter();
view.invalidate();
return true;
default:
Log.e("DragDrop Example", "Unknown action type received by OnDragListener.");
break;
}
return false;
}
}
提前致谢
我也找不到使用 DialogFragment 的方法,所以我找到了解决方法。 DialogFragments 的问题是当对话框被关闭时视图被破坏,因此您遇到了 NPE。为了解决这个问题,我在底层片段的视图中添加了一个看起来像对话框的视图(使用 CardView)。当 "dialog" 需要显示时,我将可见性设置为 View.VISIBLE,当它需要隐藏时,我将其设置为 View.GONE。这使您正在拖动的视图与片段保持在同一上下文中,并且在隐藏 "dialog" 时不会被销毁。