Android 上无法使用 Imageview 显示图像

发布于 2024-12-17 07:03:25 字数 1109 浏览 3 评论 0原文

在我的 \drawable-mdpi 文件夹中,我有一个名为: test.jpg 的图像

在我的 main.xml 文件的 LinearLayout 部分中,我有:

<ImageView 
   android:id="@+id/test_image"
   android:src="@drawable/test"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   />

在我的 src 文件夹中,我只有 1 个文件 HelloAndroidActivity.java 仅包含以下方法:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ImageView image = new ImageView(this);
    image = (ImageView) findViewById(R.id.test_image);
    setContentView(image);
}

这似乎是正确的,但每当我尝试运行它时,我都会得到应用程序 HelloAndroid (进程 xxxxx) 意外停止。请再试一次。

奇怪的是,它以前确实显示了图像,但现在不会,我不知道为什么。

另外,当我注释掉 ImageDisplay 代码并将其替换为 TextDisplay 代码时。即

    TextView tv = new TextView(this);
    tv.setText("Does this work?");
    setContentView(tv);

它确实显示文本。

编辑:被要求发布 logcat。 pastebin 链接

In my \drawable-mdpi folder, I have an image named: test.jpg

In my main.xml file, in my LinearLayout section, I have:

<ImageView 
   android:id="@+id/test_image"
   android:src="@drawable/test"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   />

In my src folder, I have only 1 file, HelloAndroidActivity.java with only the following method:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ImageView image = new ImageView(this);
    image = (ImageView) findViewById(R.id.test_image);
    setContentView(image);
}

This seems to be correct, yet whenever I try to run it, I get The application HelloAndroid (process xxxxx) has stopped unexpectedly. Please try again.

The strange part is it previously did display the image, but now it won't and I don't know why.

Also, when I comment out the ImageDisplay code, and replace it with TextDisplay code. i.e.

    TextView tv = new TextView(this);
    tv.setText("Does this work?");
    setContentView(tv);

It does display the text.

Edit: was asked to post logcat. Link to pastebin.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷爱 2024-12-24 07:03:25

这听起来有些刺耳,但此时我会从你手中拿走刀,并说你应该多读一点或者其他什么:

setContentView(R.layout.main);
ImageView image = new ImageView(this);
image = (ImageView) findViewById(R.id.test_image);
setContentView(image);

你已经将内容视图设置为 xml 布局。这很酷。然后,您在代码中创建一个新的 ImageView 实例,这没问题,但可能有点超出您刚才的技能水平。

然后,您可以使用从 Android 通过原始 setContentView 调用为您扩展的布局中提取的 ImageView 来覆盖刚刚创建的 ImageView。这毫无意义。就这样做吧。

setContentView(R.layout.main);
ImageView image = (ImageView) findViewById(R.id.test_image);

最后,您使用从当前活动的内容视图中提取的图像引用覆盖整个活动的内容视图。不要这样做。这毫无意义。我的猜测是,您的例外情况是您正在重置内容视图,或者您正在尝试使用另一个视图中已存在的视图来设置内容视图。

您是否尝试动态设置显示的图像?如果没有,如果您只是想“显示”图像,请执行此操作。

setContentView(R.layout.main);

就是这样。

This is going to sound harsh, but this is when I take the knife out of your hand and say you should do a little more reading or whatever:

setContentView(R.layout.main);
ImageView image = new ImageView(this);
image = (ImageView) findViewById(R.id.test_image);
setContentView(image);

You've set the content view to an xml layout. That's cool. Then you create a new ImageView instance in code, which is OK but probably a little over your skill level just now.

You then overwrite the ImageView you just created with one you pulled from the layout that Android has inflated for you from your original setContentView call. This makes no sense. Just do this.

setContentView(R.layout.main);
ImageView image = (ImageView) findViewById(R.id.test_image);

Finally, you overwrite the content view for the whole Activity with the image reference you pulled from within the content view for the current Activity. Do not do this. It makes no sense. My guess is that your exception is either that you're resetting the content view, or that you're trying to set the content view with a view that already exists in another view.

Are you trying to dynamically set the image that shows up? If not, if you're just trying to "show" the image, do this.

setContentView(R.layout.main);

That's it.

千紇 2024-12-24 07:03:25

setContentViewfindViewById 不会做您认为它们会做的事情。

setContentView 将给定视图设置为 Activity 的顶级根视图。通常,您将其设置为定义许多嵌套视图的层次结构的布局。如果你给它一个布局资源,该布局将被扩展到新的视图中。如果您给它一个现有视图,则将使用该现有视图实例。

Activity#findViewById 从根开始,在 Activity 窗口的当前视图层次结构中执行树搜索。如果它可以在层次结构中的某个位置找到具有给定 id 的视图,则返回该视图。如果找不到,该函数将返回 null。

因此,考虑到这些事情,让我们逐步完成您的代码:

setContentView(R.layout.main); - 膨胀您的主布局并将其设置为您的活动内容。到目前为止看起来不错。

ImageView image = new ImageView(this); - 创建一个新的 ImageView。这在技术上没有问题,但可能不是您想要的,因为...

image = (ImageView) findViewById(R.id.test_image); - 搜索当前视图层次结构(您在其中膨胀 R 的视图层次结构)上面的.layout.main)用于具有 id test_image 的视图。这会覆盖您的 image 变量,并丢弃您在上面一行中创建的要进行垃圾收集的new ImageView

setContentView(image); - 尝试将 id test_image 的视图设置为活动视图层次结构的根,覆盖您在第一次 setContentView 调用中膨胀的层次结构。我猜测,如果您检查异常中的堆栈跟踪,您的应用程序就会崩溃,因为此时 image 已经有一个父视图,并且您试图为其提供一个新视图而不删除它首先从当前的父级开始。

将视图附加到层次结构后,内容更改时无需重新添加它。如果您想要这样做,只需就地更改内容即可。多次调用 setContentView 是一件非常不寻常的事情。在绝大多数情况下这是不必要的。

由于您的主布局已经在 ImageView 中指定了您想要的可绘制对象,因此您的 onCreate 可以缩短为仅前两行以简单地显示图像:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.id.main);
}

setContentView and findViewById don't do what you think they do.

setContentView sets the given view as the top-level, root view of your activity. Usually you set it to a layout that defines a hierarchy of many nested views. If you give it a layout resource, that layout will be inflated into new views. If you give it an existing view, that existing view instance will be used.

Activity#findViewById performs a tree search across the current view hierarchy for your activity's window, starting at the root. If it can find a view somewhere in the hierarchy with the given id, that view is returned. If it can't be found the function returns null.

So with those things in mind, let's step through your code:

setContentView(R.layout.main); - Inflates your main layout and sets it as your activity content. Looks good so far.

ImageView image = new ImageView(this); - Creates a new ImageView. Nothing technically wrong with this but probably not what you want because...

image = (ImageView) findViewById(R.id.test_image); - Searches through the current view hierarchy (the one where you inflated R.layout.main above) for a View with the id test_image. This overwrites your image variable, throwing out the new ImageView you created on the line above to be garbage collected.

setContentView(image); - Attempts to set the view with id test_image as the root of your activity's view hierarchy, overwriting the hierarchy you inflated in your first setContentView call. I'm guessing that if you check the stack trace from the exception, your app is crashing because at this point image already has a parent view and you're trying to give it a new one without removing it from its current parent first.

Once a view has been attached to the hierarchy you do not need to re-add it when content changes. Simply change the content in-place if that's what you want to do. Calling setContentView more than once is a very unusual thing to do. In the vast majority of cases it's unnecessary.

Since your main layout already specifies the drawable you want in your ImageView your onCreate can be shortened to just the first two lines to simply show the image:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.id.main);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文