2010-11-08 150 views
1

我寫了一個android應用程序,迄今爲止它的工作完美,除了在一部手機上。這是我在兩部手機上使用2.2的確切手機,而我的作品非常完美。另一部手機一直關閉,但只有在打開查詢我的圖像數據庫並嘗試顯示圖庫的活動時纔會關閉。每個活動都在同一個地方崩潰,viewimages。起初,我認爲這是做這件事的意圖,但其中一項活動沒有意圖,它會在圖庫下面打開圖像。這適用於仿真器,我的摩托機器人和其他幾個不同的機器人。這是畫廊和我的位圖解碼器的代碼。錯誤在那之下。如果任何人都能看到任何會使它崩潰的事情,我會非常感激這個幫助。Android java.lang.RuntimeException:無法啓動活動ComponentInfo

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

以下是錯誤

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 
+2

哪一行是LivestockDetails的第182行? – 2010-11-08 22:08:11

+0

根據Eclipse,它是:\t \t column_index = cursor.getColumnIndex(ImageProvider._ID); – Opy 2010-11-09 13:54:20

+0

事實上,它強制的行關閉在另一頁我也得到了錯誤報告。 請記住它在幾個手機上工作得很好,它正在崩潰的是我的工作正常。 – Opy 2010-11-09 14:15:57

回答

1

我終於想通了。我的內容提供商被搞砸了。我有兩個應用程序,一個用於測試,一個用於發佈。我有發佈的內容提供商指向我的測試應用程序,所以在模擬器中它工作得很好,因爲內容提供商在那裏,但是當有人偷我的應用程序的內容提供商丟失。如果只有錯誤報告給出了更多信息。當我在模擬器上覆制它時,通過卸載所有內容,然後只安裝主應用程序,logcat告訴我無法找到內容提供者。這就是爲什麼它每次都在光標上崩潰。

相關問題