gpt4 book ai didi

c# - 指定的子项已有父项。您必须首先对 child 的 parent 调用 removeView()。 (C#)

转载 作者:搜寻专家 更新时间:2023-11-01 09:46:03 24 4
gpt4 key购买 nike

我见过很多与此类似的问题,但到目前为止,我所看到的一切都没有解决我的问题(主要是因为它是在 Java 中,而我使用的是 C#)。

protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.MainV2);

if (IsThereAnAppToTakePictures())
{
CreateDirectoryForPictures();
btnAdd = FindViewById<Button>(Resource.Id.btnAdd);
btnAdd.Click += DialogAddPhoto;
}

myFileListAdapter = new ArrayAdapter(this, Resource.Drawable.style_listview);
listViewFiles = FindViewById<ListView>(Resource.Id.listView1);
}

private void DialogAddPhoto(object sender, EventArgs eventArgs)
{
// passing intent i.e. REQUEST_CAMERA, SELECT_FILE
}

哪里出了问题...

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
_imageView = new ImageView(this);
btnRemove = FindViewById<Button>(Resource.Id.btnRemove);

if (resultCode == Result.Ok)
{
if (requestCode == REQUEST_CAMERA)
{
// ................. //
}
else if ((requestCode == SELECT_FILE) && (data != null))
{
Android.Net.Uri uri = data.Data;
string imgUri = Convert.ToString(uri);

string[] proj = { MediaStore.Images.ImageColumns.Data };
var cursor = ContentResolver.Query(uri, proj, null, null, null);
var colIndex = cursor.GetColumnIndex(MediaStore.Images.ImageColumns.Data);
cursor.MoveToFirst();
BitmapFactory.Options options = new BitmapFactory.Options { InJustDecodeBounds = true };
BitmapFactory.DecodeFile(cursor.GetString(colIndex), options);
int height = 300;
int width = Resources.DisplayMetrics.WidthPixels;
int outHeight = options.OutHeight;
int outWidth = options.OutWidth;
int inSampleSize = 1;

if (outHeight > height || outWidth > width)
{
inSampleSize = outWidth > outHeight
? outHeight / height
: outWidth / width;
}

options.InSampleSize = inSampleSize;
options.InJustDecodeBounds = false;
App.bm = BitmapFactory.DecodeFile(cursor.GetString(colIndex), options);
System.IO.MemoryStream mem = new System.IO.MemoryStream();
App.bm.Compress(Bitmap.CompressFormat.Png, 100, mem);
//byte[] byteArray = mem.ToArray();
_imageView.SetImageBitmap(App.bm);
App.bm = null;
GC.Collect();

myFileListAdapter.Add(imgUri);
myFileListAdapter.NotifyDataSetChanged();
setListViewHeightBasedOnChildren(listViewFiles);

listViewFiles.ItemClick += (object sender, ItemClickEventArgs e) =>
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.SetTitle("Preview");
builder.SetView(_imageView);
builder.SetCancelable(false);
builder.SetPositiveButton("Cancel", (senderAlert, args) => { });
builder.SetNegativeButton("Delete?", (senderAlert, args) =>
{

Java.Lang.Object toRemove = myFileListAdapter.GetItem(e.Position);
myFileListAdapter.Remove(toRemove);
myFileListAdapter.NotifyDataSetChanged();
setListViewHeightBasedOnChildren(listViewFiles);

});

builder.Show();
};

}
}
}

这是罪魁祸首。

builder.SetView(_imageView);

我尝试这样做,但仍然出现同样的错误。

builder.SetView(null);
builder.SetView(_imageView);

它所做的是在从图库中选择或添加图像后,将 Uri 添加到列表 (ListView)。单击该项目时,将在显示图像 (_imageView) 的位置出现警告对话框。单击 Cancel 并在 Deleting and Adding 图库中查看 Uri 图像后发生错误。

我见过的所有解决方案都使用 Inflate,但老实说,我不知道如何将其合并到我的代码中,因为 imageview 取决于动态值(从图库中导入的图像)。

请注意,我使用的是 Xamarin Android。

跟踪:

06-27 17:51:54.434 E/mono-rt (16678): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
06-27 17:51:54.434 E/mono-rt (16678): at android.view.ViewGroup.addViewInner(ViewGroup.java:3430)
06-27 17:51:54.434 E/mono-rt (16678): at android.view.ViewGroup.addView(ViewGroup.java:3301)
06-27 17:51:54.434 E/mono-rt (16678): at android.view.ViewGroup.addView(ViewGroup.java:3277)
06-27 17:51:54.434 E/mono-rt (16678): at com.android.internal.app.AlertController.setupView(AlertController.java:416)
06-27 17:51:54.434 E/mono-rt (16678): at com.android.internal.app.AlertController.installContent(AlertController.java:244)
06-27 17:51:54.434 E/mono-rt (16678): at android.app.AlertDialog.onCreate(AlertDialog.java:337)
06-27 17:51:54.434 E/mono-rt (16678): at android.app.Dialog.dispatchOnCreate(Dialog.java:355)
06-27 17:51:54.434 E/mono-rt (16678): at android.app.Dialog.show(Dialog.java:260)
06-27 17:51:54.434 E/mono-rt (16678): at android.app.AlertDialog$Builder.show(AlertDialog.java:951)
06-27 17:51:54.434 E/mono-rt (16678): at mono.android.widget.AdapterView_OnItemClickListenerImplementor.n_onItemClick(Native Method)
06-27 17:51:54.434 E/mono-rt (16678): at mono.android.widget.AdapterView_OnItemClickListenerImplementor.onItemClick(AdapterView_OnItemClickListenerImplementor.java:29)
06-27 17:51:54.434 E/mono-rt (16678): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
06-27 17:51:54.434 E/mono-rt (16678): at android.widget.AbsListView.performItemClick(AbsListView.java:1128)
06-27 17:51:54.434 E/mono-rt (16678): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2815)
06-27 17:51:54.434 E/mono-rt (16678): at android.widget.AbsListView$1.run(AbsListView.java:3574)
06-27 17:51:54.434 E/mono-rt (16678): at android.os.Handler.handleCallback(Handler.java:800)
06-27 17:51:54.434 E/mono-rt (16678): at android.os.Handler.dispatchMessage(Handler.java:100)
06-27 17:51:54.434 E/mono-rt (16678): at android.os.Looper.loop(Looper.java:194)
06-27 17:51:54.434 E/mono-rt (16678): at android.app.ActivityThread.main(ActivityThread.java:5371)
06-27 17:51:54.434 E/mono-rt (16678): at java.lang.reflect.Method.invokeNative(Native Method)
06-27 17:51:54.434 E/mono-rt (16678): at java.lang.reflect.Method.invoke(Method.java:525)
06-27 17:51:54.434 E/mono-rt (16678): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
06-27 17:51:54.434 E/mono-rt (16678): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
06-27 17:51:54.434 E/mono-rt (16678): at dalvik.system.NativeStart.main(Native Method)

最佳答案

ImageView 已经分配给父 View 创建一个新的 ImageView 并设置它

  AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.SetTitle("Preview");
var temp= new ImageView(this);
temp.SetImageBitmap(App.bm);
builder.SetView(temp);
builder.SetCancelable(false);
builder.SetPositiveButton("Cancel", (senderAlert, args) => { });
builder.SetNegativeButton("Delete?", (senderAlert, args) =>
{

Java.Lang.Object toRemove = myFileListAdapter.GetItem(e.Position);
myFileListAdapter.Remove(toRemove);
myFileListAdapter.NotifyDataSetChanged();
setListViewHeightBasedOnChildren(listViewFiles);

});

builder.Show();
};

关于c# - 指定的子项已有父项。您必须首先对 child 的 parent 调用 removeView()。 (C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38051508/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com