Gallery小部件问题

时间:2011-08-23 14:19:18

标签: android android-widget gallery imageview

logcat错误:

08-24 11:16:19.760: ERROR/AndroidRuntime(6254): FATAL EXCEPTION: main
08-24 11:16:19.760: ERROR/AndroidRuntime(6254): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ibssistemas.ibs/com.ibssistemas.ibs.gallery}:  java.lang.NullPointerException
08-24 11:16:19.760: ERROR/AndroidRuntime(6254):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-24 11:16:19.760: ERROR/AndroidRuntime(6254):     at   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-24 11:16:19.760: ERROR/AndroidRuntime(6254):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-24 11:16:19.760: ERROR/AndroidRuntime(6254):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-24 11:16:19.760: ERROR/AndroidRuntime(6254):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-24 11:16:19.760: ERROR/AndroidRuntime(6254):     at android.os.Looper.loop(Looper.java:123)
08-24 11:16:19.760: ERROR/AndroidRuntime(6254):     at android.app.ActivityThread.main(ActivityThread.java:4627)
08-24 11:16:19.760: ERROR/AndroidRuntime(6254):     at java.lang.reflect.Method.invokeNative(Native Method)
08-24 11:16:19.760: ERROR/AndroidRuntime(6254):     at java.lang.reflect.Method.invoke(Method.java:521)
08-24 11:16:19.760: ERROR/AndroidRuntime(6254):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-24 11:16:19.760: ERROR/AndroidRuntime(6254):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-24 11:16:19.760: ERROR/AndroidRuntime(6254):     at dalvik.system.NativeStart.main(Native Method)
08-24 11:16:19.760: ERROR/AndroidRuntime(6254): Caused by: java.lang.NullPointerException
08-24 11:16:19.760: ERROR/AndroidRuntime(6254):     at com.ibssistemas.ibs.gallery$ImageAdapter.getCount(gallery.java:46)
08-24 11:16:19.760: ERROR/AndroidRuntime(6254):     at  android.widget.AbsSpinner.setAdapter(AbsSpinner.java:113)
08-24 11:16:19.760: ERROR/AndroidRuntime(6254):     at com.ibssistemas.ibs.gallery.onCreate(gallery.java:35)
08-24 11:16:19.760: ERROR/AndroidRuntime(6254):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-24 11:16:19.760: ERROR/AndroidRuntime(6254):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-24 11:16:19.760: ERROR/AndroidRuntime(6254):     ... 11 more

代码(有点广泛:()

public class gallery extends Activity {
public static List<Produto> produtos;
ImageView grande;
TextView tvRef;
TextView tvDsc;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.galeria);
    tvDsc = (TextView)findViewById(R.id.tvGalDescricao);
    tvRef = (TextView)findViewById(R.id.tvGalReferencia);
    grande = (ImageView)findViewById(R.id.ivGalGrande);
    Gallery mG = (Gallery)findViewById(R.id.myGallery);
    mG.setAdapter(new ImageAdapter(this));
    produtos = ImagesGlobal.getImages();
}

class ImageAdapter extends BaseAdapter {

    private Context myContext;

    public ImageAdapter(Context c) { this.myContext = c; }

    /** Returns the amount of images we have defined. */
    public int getCount() { return produtos.size(); }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView i = new ImageView(this.myContext);

        Produto p = produtos.get(position);
        Bitmap bm = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"/IBS/Imagens/"+p.getCD_REF()+".jpg");
        i.setImageBitmap(bm);
        grande.setImageBitmap(bm);
        tvRef.setText(p.getCD_REF());
        tvDsc.setText(p.getDS_PROD());
        /* Image should be scaled as width/height are set. */
        i.setScaleType(ImageView.ScaleType.FIT_XY);
        /* Set the Width/Height of the ImageView. */
        i.setLayoutParams(new Gallery.LayoutParams(150, 150));
        return i;
    }

    public float getScale(boolean focused, int offset) {
        return Math.max(0, 1.0f / (float)Math.pow(2, Math.abs(offset)));
    }
}
}

可能导致错误的原因是什么? Thx提前。

P.S。:图像存在于文件夹中,并且对象不为空:(

2 个答案:

答案 0 :(得分:2)

在创建Gallery

对象之前放置此行 像这样

produtos = ImagesGlobal.getImages();
Gallery mG = (Gallery)findViewById(R.id.myGallery);
mG.setAdapter(new ImageAdapter(this));

答案 1 :(得分:0)

at com.ibssistemas.ibs.gallery$ImageAdapter.getCount(gallery.java:46) 检查你的gallery.java第46行。有一个NullPointerException。看起来你试图读取或写入一个空变量。