Android Hello GridView教程不会显示图像

时间:2011-07-24 07:38:11

标签: android android-layout textview android-gridview

第一次海报!

我是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目标1.5,并在我的Droid X和使用Android模拟器上尝试过这个程序。我有我的所有导入和代码编译并运行良好。但是,我的活动只显示黑屏;没有图像出现。当我点击黑色背景时,橙色方块出现在图像所在的位置。我找到了一两个提到这个问题的帖子,但它总是伴随着一些更大,更明显的问题,从未解决过。我将在下面列出我的代码和LogCat以供参考。

我注意到我的LogCat提到了:

WARN/ImageView(364): Unable to find resource: 2130837507

这让我想到了这个主题: Why setImageResource displays nothing? 使用setImageDrawable而不是setImageResource详细说明可能的解决方法。我使用以下代码行实现了这个:

imageView.setImageDrawable(mContext.getResources().getDrawable(mThumbIds[position]));

但是,这导致了一个资源$ NotFoundException崩溃了我的代码(也在下面记录)。

结论
我认为Android教程不应该需要解决方法,所以我猜我有一些配置问题。经过几个小时的搜索和尝试,我还没有找到解决方案,所以我认为这需要一些讨论。另外,这是我的第一篇文章,如果你看到任何明显的失礼,请告诉我。我提前为即将到来的墙壁代码道歉,尽管我认为数据太少可能会比太多更糟糕。 :d

谢谢大家!

MarnBeast


更新
我尝试在我的mThumbIds中使用R.drawable.icon,在我和我的一张照片之间交替。没有任何图片显示,但图标显示。然后我尝试了我的图片的.png版本,但这也不起作用。我复制了icon.png图片并在paint中编辑了它(在android上留下了小胡子:D)并将其保存为dummyicon.png。我在那个和图标之间交替,但这也不起作用。最后,我摆脱了所有的图标引用,只是使用了我的png图像,但奇怪的是,当运行时,所有图像都显示为图标,即使我没有在mThumbIds中引用它!这与先前的行为不同,当什么都没有显示时。我改变了一对虚拟图标,当运行时,那些图像只是没有显示/只是黑色。所有其他图像(在mThumbIds中重复的一个png转换图像)仍然显示为图标。

最后,我再次用图标替换了我转换后的png图像引用,在icon和dummyicon之间交替显示,现在它们都没有显示 - 只是再次出现黑屏。因此看起来,虚拟图标和图标在引用时都不会产生任何效果,只是一个黑色空间,但是我转换后的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
};

这是模拟器的输出:

Emulator Display - GridView Problem


解决方案!
事实证明,出于某些原因,本教程对于JPEG图像不起作用。此外,我做了一个愚蠢的假设,只是简单地将扩展名从.jpg改为.png,这仍然适用于图像浏览器,但仍然被Android认可为jpg。现在我已经在绘画中编辑了每张图片,然后保存为PNG类型,一切似乎都运行良好。

HOWEVER 我仍然觉得这里有问题。我假设android应该处理.jpg图像,因为他们提供了jpegs作为他们的样本图片。如果有人知道为什么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资源不是Fount异常:

    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;
    }
}

4 个答案:

答案 0 :(得分:3)

我们可以使用此代码,

public class MyGridView extends Activity { 

@Override 
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.animalsgridview);
GridView gridview = (GridView) findViewById(R.id.gridviewid);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        int imageId = ((ImageAdapter)parent.getAdapter()).mygetItemId(position);

        Intent fullScreenIntent = new Intent(v.getContext(),Full.class);
        fullScreenIntent.putExtra(Full.class.getName(),imageId);
        AnimalsGridView.this.startActivity(fullScreenIntent);         
        }
    });
}
private class ImageAdapter extends BaseAdapter {
    private Context mContext;
    public ImageAdapter(Context c) {
    mContext = c;
    }
    public int getCount() {
        return imagearray.length;
    }
    public Object getItem(int position) {
        return null;
    }
    public long getItemId(int position) {
        return imagearray[position];
    }

    // 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(150, 150));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setPadding(1, 1, 1, 1);

        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(imagearray[position]);
        return imageView;
    }
    // references to our images                    
    private Integer[] imagearray = {                 

        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

        };
    public int mygetItemId(int position) {
                return imagearray[position];
    }                                
  }
}   

这是xml代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_weight="1"
  android:layout_height="fill_parent">
<GridView
    android:id="@+id/gridviewid"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:numColumns="auto_fit"
    android:columnWidth="60dp"
    android:stretchMode="columnWidth"
    android:padding="5dp"
    android:verticalSpacing="5dp"
    android:horizontalSpacing="20dp"        
    android:gravity="center_horizontal"
    android:background="#ffffff"/>    

答案 1 :(得分:2)

用给定的代码替换你的适配器类

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 mThumbIds[position];
    }

    public long getItemId(int position) {
        return position;
    }

    // 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
    };
}

答案 2 :(得分:0)

通常,在这些情况下,清理和重建项目将解决问题。替换图像可能只是强制您的R文件刷新。

如果没有,我唯一的另一个想法是android只支持基线和渐进式jpeg。如果你的jpeg对它有一些奇怪的优化,那可能会导致资源无效。

答案 3 :(得分:0)

请尝试清理/重建项目