无法在Android上使用Imageview显示图像(Can't display image with Imageview on Android)

在我的\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文件夹中,我只有一个文件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.

最满意答案

这听起来很刺耳,但是当我从你的手中取出刀并说你应该多做一些阅读或其他事情时:

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

您已将内容视图设置为xml布局。 这很酷。 然后你在代码中创建一个新的ImageView实例,这是好的,但可能刚刚超过你的技能水平。

然后,您使用从原始setContentView调用中为您充气的布局中提取的ImageView覆盖您刚刚创建的ImageView。 这毫无意义。 就这样做吧。

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

最后,使用从当前Activity的内容视图中提取的图像引用覆盖整个Activity的内容视图。 不要这样做。 这个不成立。 我的猜测是,您的异常要么是重置内容视图,要么是您尝试使用另一个视图中已存在的视图设置内容视图。

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

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.

无法在Android上使用Imageview显示图像(Can't display image with Imageview on Android)

在我的\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文件夹中,我只有一个文件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.

最满意答案

这听起来很刺耳,但是当我从你的手中取出刀并说你应该多做一些阅读或其他事情时:

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

您已将内容视图设置为xml布局。 这很酷。 然后你在代码中创建一个新的ImageView实例,这是好的,但可能刚刚超过你的技能水平。

然后,您使用从原始setContentView调用中为您充气的布局中提取的ImageView覆盖您刚刚创建的ImageView。 这毫无意义。 就这样做吧。

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

最后,使用从当前Activity的内容视图中提取的图像引用覆盖整个Activity的内容视图。 不要这样做。 这个不成立。 我的猜测是,您的异常要么是重置内容视图,要么是您尝试使用另一个视图中已存在的视图设置内容视图。

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

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.