第一次海报!
我是 Android 开发新手,一直在遵循 Google 的 HelloView 教程,没有出现任何问题...直到 HelloGridView 教程。由于某种原因,我无法显示任何图像,只能显示黑色背景。我最初遵循本教程:
http://developer.android.com/ resources/tutorials/views/hello-gridview.html
但转到这个几乎相同的:
http://developer.android.com/guide/tutorials/views/hello-gridview.html
为了通过删除 OnItemClickListener 代码来消除一些额外的复杂性并缩小我可能的问题区域。我的代码与提供的代码完全相同,逐字复制和粘贴。我什至从教程中下载了原始示例文件,并将它们放在 res\drawable 文件夹中,R 代码似乎已成功识别它们并相应地更新了其生成的代码。我正在使用 Android target 1.5,并在我的 Droid X 上和使用 Android 模拟器上尝试过这个程序。我拥有所有导入内容,代码编译并运行良好。但是,我的活动仅显示黑屏;没有图像出现。当我单击黑色背景时,橙色方块出现在图像应有的位置。我发现有一两篇文章提到了这个问题,但它总是伴随着一些更大、更明显的问题,并且从未得到解决。我将在下面列出我的代码和LogCat以供参考。
我注意到我的 LogCat 提到:
WARN/ImageView(364): Unable to find resource: 2130837507
这让我看到了这个线程:
为什么 setImageResource 不显示任何内容?
详细介绍了使用 setImageDrawable 而不是 setImageResource 的可能解决方法。我使用以下代码行实现了这一点:
imageView.setImageDrawable(mContext.getResources().getDrawable(mThumbIds[position]));
但是,这只会导致 Resources$NotFoundException 导致我的代码崩溃(也在下面记录)。
结论
我认为 Android 教程不需要解决方法,所以我猜测我遇到了某种配置问题。经过几个小时的搜索和尝试后,我还没有找到解决方案,因此我认为值得进行一些讨论。另外,这是我的第一篇文章,如果您发现任何明显的失礼,请告诉我。我提前为即将到来的墙代码表示歉意,尽管我认为数据太少可能比太多更糟糕。 :D
谢谢大家!
马恩野兽
更新
我尝试在 mThumbIds 中使用 R.drawable.icon,在它和我的一张图片之间交替使用。没有显示任何图片,但图标显示了。然后我尝试了我的图片的 .png 版本,但这也不起作用。我复制了 icon.png 图像并在 Paint 中对其进行了编辑(在 Android 家伙上留了胡子 :D)并将其保存为 dummyicon.png。我在它和图标之间交替,但这也不起作用。最后,我删除了所有图标引用,只使用了我的 png 图像,但奇怪的是,运行时,所有图像都显示为图标,即使我没有在 mThumbIds 中引用它!这与之前的行为不同,之前什么也没有显示。我将几个虚拟图标更改为虚拟图标,当运行时,这些图像没有显示/只是黑色。所有其他图像(mThumbIds 中重复的一个 png 转换图像)仍然显示为图标。
最后,我再次用图标替换了转换后的 png 图像引用,在图标和虚拟图标之间交替,现在它们都没有出现 - 只是再次出现黑屏。因此,看起来 dummyicon 和 icon 在引用时都没有产生任何结果,只是一个黑色空间,但我转换后的 png 图像 - chloie1.png - 产生了 icon.png。我添加了另一张图片 - chloie2.png - 并在 chloie1 和 chloie2 之间交替,但只有 chloie1 显示图标图像。因此,这是我的 mThumbIds 目前的状态:
private Integer[] mThumbIds = {
R.drawable.chloie1, R.drawable.chloie2,
R.drawable.chloie1, R.drawable.chloie2,
R.drawable.chloie1, R.drawable.chloie2,
R.drawable.chloie1, R.drawable.chloie2,
R.drawable.chloie1, R.drawable.chloie2,
R.drawable.chloie1, R.drawable.chloie2,
R.drawable.chloie1, R.drawable.chloie2,
R.drawable.chloie1, R.drawable.chloie2,
R.drawable.chloie1, R.drawable.chloie2,
R.drawable.chloie1, R.drawable.chloie2,
R.drawable.chloie1, R.drawable.chloie2
};
这是模拟器的输出:
解决方案!
事实证明,由于某种原因,本教程不适用于 JPEG 图像。此外,我做了一个愚蠢的假设,只是将扩展名从 .jpg 更改为 .png,它在图像查看器中仍然有效,但仍然被 Android 识别为 jpg。现在我已经在 Paint 中编辑了每张图片,然后保存为 PNG 类型,一切似乎都工作正常。
但是我仍然觉得这里有问题。我假设 android 应该处理 .jpg 图像,因为他们提供了 jpeg 作为示例图片。如果有人知道为什么 PNG 有效而 JPG 无效,请回复。同时,这个简单的修复就可以了。
感谢您的帮助!
参考
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
HelloGridViewActivity.java
package com.marnbeast.android;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class HelloGridViewActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
}
}
ImageAdapter.java
package com.marnbeast.android;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7
};
}
LogCat 无法查找资源:
07-24 06:00:04.564: WARN/ResourceType(364): getEntry failing because entryIndex 3 is beyond type entryCount 1
07-24 06:00:04.564: WARN/ResourceType(364): Failure getting entry for 0x7f020003 (t=1 e=3) in package 0: 0x80000001
07-24 06:00:04.584: WARN/ImageView(364): Unable to find resource: 2130837507
07-24 06:00:04.584: WARN/ImageView(364): android.content.res.Resources$NotFoundException: Resource ID #0x7f020003
07-24 06:00:04.584: WARN/ImageView(364): at android.content.res.Resources.getValue(Resources.java:891)
07-24 06:00:04.584: WARN/ImageView(364): at android.content.res.Resources.getDrawable(Resources.java:579)
07-24 06:00:04.584: WARN/ImageView(364): at android.widget.ImageView.resolveUri(ImageView.java:485)
07-24 06:00:04.584: WARN/ImageView(364): at android.widget.ImageView.setImageResource(ImageView.java:270)
07-24 06:00:04.584: WARN/ImageView(364): at com.marnbeast.android.ImageAdapter.getView(ImageAdapter.java:41)
07-24 06:00:04.584: WARN/ImageView(364): at android.widget.AbsListView.obtainView(AbsListView.java:1274)
07-24 06:00:04.584: WARN/ImageView(364): at android.widget.GridView.onMeasure(GridView.java:934)
07-24 06:00:04.584: WARN/ImageView(364): at android.view.View.measure(View.java:7964)
07-24 06:00:04.584: WARN/ImageView(364): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
07-24 06:00:04.584: WARN/ImageView(364): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
07-24 06:00:04.584: WARN/ImageView(364): at android.view.View.measure(View.java:7964)
07-24 06:00:04.584: WARN/ImageView(364): at android.widget.LinearLayout.measureVertical(LinearLayout.java:464)
07-24 06:00:04.584: WARN/ImageView(364): at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
07-24 06:00:04.584: WARN/ImageView(364): at android.view.View.measure(View.java:7964)
07-24 06:00:04.584: WARN/ImageView(364): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
07-24 06:00:04.584: WARN/ImageView(364): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
07-24 06:00:04.584: WARN/ImageView(364): at android.view.View.measure(View.java:7964)
07-24 06:00:04.584: WARN/ImageView(364): at android.view.ViewRoot.performTraversals(ViewRoot.java:763)
07-24 06:00:04.584: WARN/ImageView(364): at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
07-24 06:00:04.584: WARN/ImageView(364): at android.os.Handler.dispatchMessage(Handler.java:99)
07-24 06:00:04.584: WARN/ImageView(364): at android.os.Looper.loop(Looper.java:123)
07-24 06:00:04.584: WARN/ImageView(364): at android.app.ActivityThread.main(ActivityThread.java:4363)
07-24 06:00:04.584: WARN/ImageView(364): at java.lang.reflect.Method.invokeNative(Native Method)
07-24 06:00:04.584: WARN/ImageView(364): at java.lang.reflect.Method.invoke(Method.java:521)
07-24 06:00:04.584: WARN/ImageView(364): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-24 06:00:04.584: WARN/ImageView(364): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-24 06:00:04.584: WARN/ImageView(364): at dalvik.system.NativeStart.main(Native Method)
使用 .setImageDrawable 解决方法后,LogCat 资源未出现异常:
07-24 07:02:50.234: ERROR/AndroidRuntime(390): Uncaught handler: thread main exiting due to uncaught exception
07-24 07:02:50.245: ERROR/AndroidRuntime(390): android.content.res.Resources$NotFoundException: Resource ID #0x7f020003
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at android.content.res.Resources.getValue(Resources.java:891)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at android.content.res.Resources.getDrawable(Resources.java:579)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at com.marnbeast.android.ImageAdapter.getView(ImageAdapter.java:40)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at android.widget.AbsListView.obtainView(AbsListView.java:1274)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at android.widget.GridView.onMeasure(GridView.java:934)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at android.view.View.measure(View.java:7964)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at android.view.View.measure(View.java:7964)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at android.widget.LinearLayout.measureVertical(LinearLayout.java:464)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at android.view.View.measure(View.java:7964)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at android.view.View.measure(View.java:7964)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at android.view.ViewRoot.performTraversals(ViewRoot.java:763)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at android.os.Handler.dispatchMessage(Handler.java:99)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at android.os.Looper.loop(Looper.java:123)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at android.app.ActivityThread.main(ActivityThread.java:4363)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at java.lang.reflect.Method.invokeNative(Native Method)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at java.lang.reflect.Method.invoke(Method.java:521)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at dalvik.system.NativeStart.main(Native Method)
编辑:我添加了 R 文件来验证图像是否被正确引用。
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/
package com.marnbeast.android;
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int icon=0x7f020000;
public static final int sample_0=0x7f020001;
public static final int sample_1=0x7f020002;
public static final int sample_2=0x7f020003;
public static final int sample_3=0x7f020004;
public static final int sample_4=0x7f020005;
public static final int sample_5=0x7f020006;
public static final int sample_6=0x7f020007;
public static final int sample_7=0x7f020008;
}
public static final class id {
public static final int gridview=0x7f050000;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class string {
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
}
}
First time poster!
I am new to Android development and have been following Google's HelloView tutorials without problems... until the HelloGridView tutorial. For some reason I cannot get any images to display, only a black background. I originally followed this tutorial:
http://developer.android.com/resources/tutorials/views/hello-gridview.html
but moved on to this nearly identical one:
http://developer.android.com/guide/tutorials/views/hello-gridview.html
in order to eliminate some added complexity by removing the OnItemClickListener code and narrow my possible problem areas. My code is exactly the same as the provided code, literally copied and pasted. I even downloaded the original sample files from the tutorial and placed them in the res\drawable folder, and the R code appears to have recognized them successfully and updated it's generated code accordingly. I am using Android target 1.5 and have tried this program both on my Droid X and using the Android emulator. I have all of my imports and the code compiles and runs fine. However, my activity displays only a black screen; no images appear. When I click on the black background, orange squares appear where the images should be. I found one or two posts mentioning this problem, but it was always coupled with some larger, more obvious problem and never addressed. I will list my code and LogCat below for reference.
I noticed that my LogCat mentioned:
WARN/ImageView(364): Unable to find resource: 2130837507
which led me to this thread:
Why setImageResource displays nothing?
detailing a possible workaround using setImageDrawable instead of setImageResource. I implemented this using the following line of code:
imageView.setImageDrawable(mContext.getResources().getDrawable(mThumbIds[position]));
however, that just resulted in a Resources$NotFoundException that crashed my code (also documented below).
IN CONCLUSION
I figure that an Android tutorial shouldn't require a workaround, so I'm guessing that I have some sort of configuration issue. After a few hours of searching and trying things, I haven't found a solution yet so I figured it warrants some discussion. Also, with this being my first post, please let me know if you see any glaring faux pas. I apologize in advance for the oncoming wall-o-code, though I figure too little data may be worse than too much. :D
Thanks Guys!
MarnBeast
UPDATE
I tried using R.drawable.icon in my mThumbIds, alternating between that and one of my pictures. None of the pictures showed up, but the icons did. I then tried a .png version of my picture, but that didn't work either. I copied the icon.png image and edited it in paint (put a mustache on the android guy :D) and saved it as dummyicon.png. I alternated between that and the icon, but that didn't work either. Finally, I got rid of all of the icon references, and just used my png image, but oddly, when run, ALL of the images showed up as the icon, even though I am not referencing it in mThumbIds! This differs from the previous behavior, when nothing at all was displayed. I changed a couple to the dummyicon, and when run, those images just didn't show up / were just black. All of the other images (the one png converted image repeated in mThumbIds) still showed up as icons.
Finally, I replaced my converted png image references with icon again, alternating between icon and dummyicon, and now neither of them show up - just a black screen again. So it appears that dummyicon and icon both yield nothing when referenced, just a black space, but my converted png image - chloie1.png - yields the icon.png. I added another picture - chloie2.png - and alternated between chloie1 and chloie2, but only chloie1 shows the icon image. Thus, this is the state of my mThumbIds as of now:
private Integer[] mThumbIds = {
R.drawable.chloie1, R.drawable.chloie2,
R.drawable.chloie1, R.drawable.chloie2,
R.drawable.chloie1, R.drawable.chloie2,
R.drawable.chloie1, R.drawable.chloie2,
R.drawable.chloie1, R.drawable.chloie2,
R.drawable.chloie1, R.drawable.chloie2,
R.drawable.chloie1, R.drawable.chloie2,
R.drawable.chloie1, R.drawable.chloie2,
R.drawable.chloie1, R.drawable.chloie2,
R.drawable.chloie1, R.drawable.chloie2,
R.drawable.chloie1, R.drawable.chloie2
};
and this is the output from the emulator:
SOLUTION!
As it turned out, for some reason this tutorial would not work for me with JPEG images. Furthermore, I made a dumb assumption and simply changed the extension from .jpg to .png, which still worked in image viewers but was still recognized as a jpg by android. Now that I have edited each picture in paint, then saved as a PNG type, everything seems to work fine.
HOWEVER I still feel that there is an issue here. I'm assuming that android should handle .jpg images considering that they provided jpegs as their sample pictures. If anyone has an idea as to why PNG works but JPG doesn't, please respond. In the mean time, this simple fix will do just fine.
Thanks for the help!
REFERENCES
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
HelloGridViewActivity.java
package com.marnbeast.android;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class HelloGridViewActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
}
}
ImageAdapter.java
package com.marnbeast.android;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7
};
}
LogCat Unable to Find Resource:
07-24 06:00:04.564: WARN/ResourceType(364): getEntry failing because entryIndex 3 is beyond type entryCount 1
07-24 06:00:04.564: WARN/ResourceType(364): Failure getting entry for 0x7f020003 (t=1 e=3) in package 0: 0x80000001
07-24 06:00:04.584: WARN/ImageView(364): Unable to find resource: 2130837507
07-24 06:00:04.584: WARN/ImageView(364): android.content.res.Resources$NotFoundException: Resource ID #0x7f020003
07-24 06:00:04.584: WARN/ImageView(364): at android.content.res.Resources.getValue(Resources.java:891)
07-24 06:00:04.584: WARN/ImageView(364): at android.content.res.Resources.getDrawable(Resources.java:579)
07-24 06:00:04.584: WARN/ImageView(364): at android.widget.ImageView.resolveUri(ImageView.java:485)
07-24 06:00:04.584: WARN/ImageView(364): at android.widget.ImageView.setImageResource(ImageView.java:270)
07-24 06:00:04.584: WARN/ImageView(364): at com.marnbeast.android.ImageAdapter.getView(ImageAdapter.java:41)
07-24 06:00:04.584: WARN/ImageView(364): at android.widget.AbsListView.obtainView(AbsListView.java:1274)
07-24 06:00:04.584: WARN/ImageView(364): at android.widget.GridView.onMeasure(GridView.java:934)
07-24 06:00:04.584: WARN/ImageView(364): at android.view.View.measure(View.java:7964)
07-24 06:00:04.584: WARN/ImageView(364): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
07-24 06:00:04.584: WARN/ImageView(364): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
07-24 06:00:04.584: WARN/ImageView(364): at android.view.View.measure(View.java:7964)
07-24 06:00:04.584: WARN/ImageView(364): at android.widget.LinearLayout.measureVertical(LinearLayout.java:464)
07-24 06:00:04.584: WARN/ImageView(364): at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
07-24 06:00:04.584: WARN/ImageView(364): at android.view.View.measure(View.java:7964)
07-24 06:00:04.584: WARN/ImageView(364): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
07-24 06:00:04.584: WARN/ImageView(364): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
07-24 06:00:04.584: WARN/ImageView(364): at android.view.View.measure(View.java:7964)
07-24 06:00:04.584: WARN/ImageView(364): at android.view.ViewRoot.performTraversals(ViewRoot.java:763)
07-24 06:00:04.584: WARN/ImageView(364): at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
07-24 06:00:04.584: WARN/ImageView(364): at android.os.Handler.dispatchMessage(Handler.java:99)
07-24 06:00:04.584: WARN/ImageView(364): at android.os.Looper.loop(Looper.java:123)
07-24 06:00:04.584: WARN/ImageView(364): at android.app.ActivityThread.main(ActivityThread.java:4363)
07-24 06:00:04.584: WARN/ImageView(364): at java.lang.reflect.Method.invokeNative(Native Method)
07-24 06:00:04.584: WARN/ImageView(364): at java.lang.reflect.Method.invoke(Method.java:521)
07-24 06:00:04.584: WARN/ImageView(364): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-24 06:00:04.584: WARN/ImageView(364): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-24 06:00:04.584: WARN/ImageView(364): at dalvik.system.NativeStart.main(Native Method)
LogCat Resource Not Fount Exception After Workaround with .setImageDrawable:
07-24 07:02:50.234: ERROR/AndroidRuntime(390): Uncaught handler: thread main exiting due to uncaught exception
07-24 07:02:50.245: ERROR/AndroidRuntime(390): android.content.res.Resources$NotFoundException: Resource ID #0x7f020003
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at android.content.res.Resources.getValue(Resources.java:891)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at android.content.res.Resources.getDrawable(Resources.java:579)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at com.marnbeast.android.ImageAdapter.getView(ImageAdapter.java:40)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at android.widget.AbsListView.obtainView(AbsListView.java:1274)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at android.widget.GridView.onMeasure(GridView.java:934)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at android.view.View.measure(View.java:7964)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at android.view.View.measure(View.java:7964)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at android.widget.LinearLayout.measureVertical(LinearLayout.java:464)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at android.view.View.measure(View.java:7964)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at android.view.View.measure(View.java:7964)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at android.view.ViewRoot.performTraversals(ViewRoot.java:763)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at android.os.Handler.dispatchMessage(Handler.java:99)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at android.os.Looper.loop(Looper.java:123)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at android.app.ActivityThread.main(ActivityThread.java:4363)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at java.lang.reflect.Method.invokeNative(Native Method)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at java.lang.reflect.Method.invoke(Method.java:521)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-24 07:02:50.245: ERROR/AndroidRuntime(390): at dalvik.system.NativeStart.main(Native Method)
EDIT: I'm including the R file to verify that the images are correctly referenced.
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/
package com.marnbeast.android;
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int icon=0x7f020000;
public static final int sample_0=0x7f020001;
public static final int sample_1=0x7f020002;
public static final int sample_2=0x7f020003;
public static final int sample_3=0x7f020004;
public static final int sample_4=0x7f020005;
public static final int sample_5=0x7f020006;
public static final int sample_6=0x7f020007;
public static final int sample_7=0x7f020008;
}
public static final class id {
public static final int gridview=0x7f050000;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class string {
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
}
}
发布评论
评论(4)
我们可以使用这段代码,
这是xml代码
we can use this code,
this is the xml code
用给定的代码替换您的适配器类
replace your adapter class by given code
通常,在这些情况下,清理并重建项目即可解决问题。替换图像可能只是强制刷新 R 文件。
如果没有,我唯一的另一个想法是 android 仅支持基线和渐进式 jpeg。如果您的 jpeg 进行了某种奇怪的优化,则可能会导致资源无效。
Usually, in these cases, a clean and rebuild of the project will resolve the problem. It is possible that replacing the images simply forced your R file to refresh.
If not, my only other thought is that android only supports baseline and progressive jpegs. If your jpeg had some sort of weird optimisation on it, that could have made for an invalid resource.
请尝试清理/重建您的项目
Try making a Clean/rebuild of your project please