Android java.lang.RuntimeException:无法启动活动 ComponentInfo

发布于 2024-10-01 14:12:07 字数 6164 浏览 7 评论 0原文

我编写了一个 Android 应用程序,到目前为止,除了在一部手机上之外,它运行得非常完美。这正是我所拥有的 2.2 手机,我的手机运行完美。另一部手机一直被强行关闭,但仅在打开查询我的图像数据库并尝试显示图库的活动时才发生。每个活动都会在同一个地方崩溃,即视图图像。起初我以为这是这样做的意图,但其中一个活动没有意图,它打开了图库下方的图像。这适用于模拟器、我的摩托机器人和其他一些不同的机器人。这是画廊和我的位图解码器的代码。错误就在下面。如果有人能看到任何会导致崩溃的内容,我将非常感谢您的帮助。

public void viewimages() {

    String [] proj={ImageProvider._ID, ImageProvider.IMAGE, ImageProvider.TABLENAME,
            ImageProvider.ITEMID, ImageProvider.IMAGEDATE};

    cursor = managedQuery(ImageProvider.CONTENT_URI,
        proj,
        ImageProvider.TABLENAME + "=" + "'" + Tablename + "'"
    + " and " + ImageProvider.ITEMID + "=" + "'" + ID + "'",
        null,
        ImageProvider.IMAGEDATE);

第182行-> column_index = 游标.getColumnIndex(ImageProvider._ID);

    g = (Gallery) findViewById(R.id.gallery);
    g.setAdapter(new ImageAdapter(this));
    g.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

            cursor.moveToPosition(position);
            ImageID = cursor.getLong(column_index);
            Intent i = new Intent(LivestockDetails.this, ImageViewer.class);
            i.putExtra("id", ImageID);
            startActivity(i);
        }
    });
}

private Bitmap decodeFile(String file){
    Bitmap b = null;
    //Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(file, o);
    int scale = 1;
    if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
        scale = 2 ^ (int) Math.ceil(Math.log(IMAGE_MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5));
    }

    //Decode with inSampleSize 
    BitmapFactory.Options o2 = new BitmapFactory.Options(); 
    o2.inSampleSize = scale; 
    b = BitmapFactory.decodeFile(file, o2); 
    return b; 
}

public class ImageAdapter extends BaseAdapter {
    int mGalleryItemBackground;
    public ImageAdapter(Context c) {
        mContext = c;

        TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
        mGalleryItemBackground = a.getResourceId(
                R.styleable.Gallery1_android_galleryItemBackground, 0);
        a.recycle();
    }

    public int getCount() {
        return cursor.getCount();
    }

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

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

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView i = new ImageView(mContext);
        if (convertView == null) {

            String [] proj={ImageProvider._ID, ImageProvider.IMAGE, ImageProvider.TABLENAME, 
                    ImageProvider.ITEMID, ImageProvider.IMAGEDATE};

            cursor = managedQuery(ImageProvider.CONTENT_URI,
                proj,
                ImageProvider.TABLENAME + "=" + "'" + Tablename + "'"
                + " and " + ImageProvider.ITEMID + "=" + "'" + ID + "'",
                null,
                ImageProvider.IMAGEDATE + " DESC");

            column_path = cursor.getColumnIndex(ImageProvider.IMAGE);
            column_index = cursor.getColumnIndex(ImageProvider._ID);
            cursor.moveToPosition(position);
            String filename = cursor.getString(column_path);
            java.io.File file = new java.io.File(filename);
            if(!file.exists()) {
                ImageID = cursor.getLong(column_index);
                getContentResolver().delete(ImageProvider.CONTENT_URI,
                        ImageProvider._ID + "=" + ImageID, null);
                Toast.makeText(LivestockDetails.this,
                        "There are missing images.  They have been removed from the database." ,
                        Toast.LENGTH_LONG).show();
                viewimages();
            }
            else {                  
                Bitmap bitmapOrg = null;
                bitmapOrg = decodeFile(filename);

                Matrix matrix = new Matrix();
                matrix.postScale(scale,scale);
                resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
                        bitmapOrg.getWidth(), bitmapOrg.getHeight(), matrix, true);

                Drawable delete = i.getDrawable();
                if(delete!= null) {
                    ((BitmapDrawable)i.getDrawable()).getBitmap().recycle();
                    i.setImageBitmap(resizedBitmap);
                }
                else {
                    i.setImageBitmap(resizedBitmap);
                }
                i.setScaleType(ImageView.ScaleType.FIT_XY);
                i.setLayoutParams(new Gallery.LayoutParams(240, 180));
                i.setBackgroundResource(mGalleryItemBackground);
            }
        }
        return i;
    }
}

这是错误

java.lang.RuntimeException: Unable to start activity ComponentInfo{myaquarium.logger/myaquarium.logger.LivestockDetails}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at myaquarium.logger.LivestockDetails.viewimages(LivestockDetails.java:182)
at myaquarium.logger.LivestockDetails.onCreate(LivestockDetails.java:71)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
... 11 more

I wrote an android app and so far it works perfect except for on one phone. It is the exact phone that I have with 2.2 on both phones and mine works perfect. The other phone is getting force close all the time but only when opening an activity that queries my image DB and tries to show a gallery. Every activity crashes in the same place, the viewimages. At first I thought it was the intent that was doing it but one of the activitys does not have the intent, it opens the images below the gallery. This works on the emulator, on my moto droid and on a few other different droids. Here is the code for the gallery and my bitmap decoder. The error is below that. If anyone can see anything that would make it crash I would really appreciate the help.

public void viewimages() {

    String [] proj={ImageProvider._ID, ImageProvider.IMAGE, ImageProvider.TABLENAME,
            ImageProvider.ITEMID, ImageProvider.IMAGEDATE};

    cursor = managedQuery(ImageProvider.CONTENT_URI,
        proj,
        ImageProvider.TABLENAME + "=" + "'" + Tablename + "'"
    + " and " + ImageProvider.ITEMID + "=" + "'" + ID + "'",
        null,
        ImageProvider.IMAGEDATE);

Line 182 -> column_index = cursor.getColumnIndex(ImageProvider._ID);

    g = (Gallery) findViewById(R.id.gallery);
    g.setAdapter(new ImageAdapter(this));
    g.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

            cursor.moveToPosition(position);
            ImageID = cursor.getLong(column_index);
            Intent i = new Intent(LivestockDetails.this, ImageViewer.class);
            i.putExtra("id", ImageID);
            startActivity(i);
        }
    });
}

private Bitmap decodeFile(String file){
    Bitmap b = null;
    //Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(file, o);
    int scale = 1;
    if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
        scale = 2 ^ (int) Math.ceil(Math.log(IMAGE_MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5));
    }

    //Decode with inSampleSize 
    BitmapFactory.Options o2 = new BitmapFactory.Options(); 
    o2.inSampleSize = scale; 
    b = BitmapFactory.decodeFile(file, o2); 
    return b; 
}

public class ImageAdapter extends BaseAdapter {
    int mGalleryItemBackground;
    public ImageAdapter(Context c) {
        mContext = c;

        TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
        mGalleryItemBackground = a.getResourceId(
                R.styleable.Gallery1_android_galleryItemBackground, 0);
        a.recycle();
    }

    public int getCount() {
        return cursor.getCount();
    }

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

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

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView i = new ImageView(mContext);
        if (convertView == null) {

            String [] proj={ImageProvider._ID, ImageProvider.IMAGE, ImageProvider.TABLENAME, 
                    ImageProvider.ITEMID, ImageProvider.IMAGEDATE};

            cursor = managedQuery(ImageProvider.CONTENT_URI,
                proj,
                ImageProvider.TABLENAME + "=" + "'" + Tablename + "'"
                + " and " + ImageProvider.ITEMID + "=" + "'" + ID + "'",
                null,
                ImageProvider.IMAGEDATE + " DESC");

            column_path = cursor.getColumnIndex(ImageProvider.IMAGE);
            column_index = cursor.getColumnIndex(ImageProvider._ID);
            cursor.moveToPosition(position);
            String filename = cursor.getString(column_path);
            java.io.File file = new java.io.File(filename);
            if(!file.exists()) {
                ImageID = cursor.getLong(column_index);
                getContentResolver().delete(ImageProvider.CONTENT_URI,
                        ImageProvider._ID + "=" + ImageID, null);
                Toast.makeText(LivestockDetails.this,
                        "There are missing images.  They have been removed from the database." ,
                        Toast.LENGTH_LONG).show();
                viewimages();
            }
            else {                  
                Bitmap bitmapOrg = null;
                bitmapOrg = decodeFile(filename);

                Matrix matrix = new Matrix();
                matrix.postScale(scale,scale);
                resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
                        bitmapOrg.getWidth(), bitmapOrg.getHeight(), matrix, true);

                Drawable delete = i.getDrawable();
                if(delete!= null) {
                    ((BitmapDrawable)i.getDrawable()).getBitmap().recycle();
                    i.setImageBitmap(resizedBitmap);
                }
                else {
                    i.setImageBitmap(resizedBitmap);
                }
                i.setScaleType(ImageView.ScaleType.FIT_XY);
                i.setLayoutParams(new Gallery.LayoutParams(240, 180));
                i.setBackgroundResource(mGalleryItemBackground);
            }
        }
        return i;
    }
}

Here is the error

java.lang.RuntimeException: Unable to start activity ComponentInfo{myaquarium.logger/myaquarium.logger.LivestockDetails}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at myaquarium.logger.LivestockDetails.viewimages(LivestockDetails.java:182)
at myaquarium.logger.LivestockDetails.onCreate(LivestockDetails.java:71)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
... 11 more

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

友欢 2024-10-08 14:12:07

我终于想通了。我的内容提供商搞砸了。我有两个应用程序,一个用于测试,一个用于发布。我有用于发布的内容提供程序指向我的测试应用程序,因此在模拟器中它工作正常,因为内容提供程序在那里,但当有人购买我的应用程序时,内容提供程序丢失了。如果错误报告能提供更多信息就好了。当我通过卸载所有内容然后仅安装主应用程序将其复制到模拟器上时,logcat 告诉我无法找到内容提供程序。这就是为什么它每次都会在光标上崩溃。

I finally figured it out. My content provider was screwed up. I have two app, one is for my testing and one is for publishing. I had the content provider for publishing pointing to my testing app so in the emulator it worked fine since the content provider was there but when someone purchsed my app the conntent provider was missing. If only the error report gave more info. When I replicated it on my emulator by uninstalling everything then only installing the main app the logcat told me ythe content provider could not be found. Thats why it was crashing on the cursor every time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文