如何在 android 中动态地在 Android 上查看图像视图到线性布局
How to view an Image View to a linear layout in android dynamically on andorid
我有两个活动。一个提交数据,一个显示提交的数据。我能够将图像添加到第一个 activity 中,然后将数据存储在一个名为 "images".
的 ArrayList 实例的单例数据对象中
我遇到的问题是,当尝试从图像数组中检索图像并显示它们时,应用程序崩溃了。我试过调试代码,它运行完美,直到你到达最后一行:
attachmentLayout.addView(image,imParams);
图像正确存储在单例中,当您调试它时,它显示它们实际上在那里。
我试图自己弄明白,但找不到它是什么。我对 android 编程很陌生,因此非常感谢您的帮助。我已经添加了我的代码,试图获取下面的图像。
if(values.getImage()!=null){
int count = images.size()-1;
for (int x=0;x<=count;x++){
//declare attachments and get image from arraylist<ImageView>
LinearLayout attachmentLayout = (LinearLayout) findViewById(R.id.attachments);
ImageView image;
image = images.get(x);
//set parameters
LinearLayout.LayoutParams imParams =
new LinearLayout.LayoutParams(90,270);
image.setBottom(attachmentLayout.getBottom());
image.setVisibility(View.VISIBLE);
image.isShown();
//add to linear layout
attachmentLayout.addView(image,imParams);
}
}
编辑:
这是我在应用程序崩溃后重新启动时得到的 logcat。不过我不认为这有帮助。
08-24 12:34:18.522 4782-4820/com.example.jules_gribble.test I/Adreno﹕ QUALCOMM build : 065751b,
Build Date : 04/15/15
OpenGL ES Shader Compiler Version: E031.25.03.07
Local Branch :
Remote Branch : quic/LA.BF64.1.2.1_rb2.9
Remote Branch : NONE
Reconstruct Branch : AU_LINUX_ANDROID_LA.BF64.1.2.1_RB2.05.01.00.081.016 + 065751b + NOTHING
设法自己回答了我的问题。结果它崩溃了,因为我在提交 activity 中忽略了它被声明为
ImageView image = new ImageView(Test.this);
这意味着它不能用于不同的 activity(测试是提交 activity 的 class 的名称,它将被更改)。
为了解决这个问题,我将其更改为
ImageView image = new ImageView(Submitted.this);
其中 submitted 是提交的名称 activity。然后我需要获取图像的位图并将位图设置为新的图像视图。
Bitmap bitmap = ((BitmapDrawable)images.get(x).getDrawable()).getBitmap();
image.setImageBitmap(bitmap);
我从 HERE 中找到了这个。
希望这对以后的人有所帮助。
我有两个活动。一个提交数据,一个显示提交的数据。我能够将图像添加到第一个 activity 中,然后将数据存储在一个名为 "images".
的 ArrayList 实例的单例数据对象中我遇到的问题是,当尝试从图像数组中检索图像并显示它们时,应用程序崩溃了。我试过调试代码,它运行完美,直到你到达最后一行:
attachmentLayout.addView(image,imParams);
图像正确存储在单例中,当您调试它时,它显示它们实际上在那里。
我试图自己弄明白,但找不到它是什么。我对 android 编程很陌生,因此非常感谢您的帮助。我已经添加了我的代码,试图获取下面的图像。
if(values.getImage()!=null){
int count = images.size()-1;
for (int x=0;x<=count;x++){
//declare attachments and get image from arraylist<ImageView>
LinearLayout attachmentLayout = (LinearLayout) findViewById(R.id.attachments);
ImageView image;
image = images.get(x);
//set parameters
LinearLayout.LayoutParams imParams =
new LinearLayout.LayoutParams(90,270);
image.setBottom(attachmentLayout.getBottom());
image.setVisibility(View.VISIBLE);
image.isShown();
//add to linear layout
attachmentLayout.addView(image,imParams);
}
}
编辑: 这是我在应用程序崩溃后重新启动时得到的 logcat。不过我不认为这有帮助。
08-24 12:34:18.522 4782-4820/com.example.jules_gribble.test I/Adreno﹕ QUALCOMM build : 065751b,
Build Date : 04/15/15
OpenGL ES Shader Compiler Version: E031.25.03.07
Local Branch :
Remote Branch : quic/LA.BF64.1.2.1_rb2.9
Remote Branch : NONE
Reconstruct Branch : AU_LINUX_ANDROID_LA.BF64.1.2.1_RB2.05.01.00.081.016 + 065751b + NOTHING
设法自己回答了我的问题。结果它崩溃了,因为我在提交 activity 中忽略了它被声明为
ImageView image = new ImageView(Test.this);
这意味着它不能用于不同的 activity(测试是提交 activity 的 class 的名称,它将被更改)。
为了解决这个问题,我将其更改为
ImageView image = new ImageView(Submitted.this);
其中 submitted 是提交的名称 activity。然后我需要获取图像的位图并将位图设置为新的图像视图。
Bitmap bitmap = ((BitmapDrawable)images.get(x).getDrawable()).getBitmap();
image.setImageBitmap(bitmap);
我从 HERE 中找到了这个。
希望这对以后的人有所帮助。