【问题标题】:Problem with Gallery widget图库小部件的问题
【发布时间】:2011-08-23 14:19:17
【问题描述】:

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

什么可能导致错误?提前谢谢。

P.S.:图片存在文件夹中,对象不为空:(

【问题讨论】:

  • 它是一个 类型的列表,其中包含所有引用的变量。

标签: android android-widget gallery imageview


【解决方案1】:

在创建 Gallery 对象之前添加这一行

喜欢这个

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

【讨论】:

  • 这是因为 getCount 一创建 Adapter 就被调用,因此 produtos 变量必须在 Adapter 之前初始化。
【解决方案2】:

at com.ibssistemas.ibs.gallery$ImageAdapter.getCount(gallery.java:46) 检查你的 gallery.java 第 46 行。有一个 NullPointerException。您似乎在尝试读取或写入空变量。

【讨论】:

    猜你喜欢
    • 2011-05-03
    • 2016-08-21
    • 2010-10-15
    • 1970-01-01
    • 2019-06-25
    • 1970-01-01
    • 2019-02-10
    • 1970-01-01
    • 2022-12-29
    相关资源
    最近更新 更多