2012-08-17 77 views
0

我试图在Android应用上显示SQLite的个人资料Contact,但它不起作用,它在image.setImageBitmap(bitmap)上崩溃,出现.NullPointerException,我不知道为什么。从SQLite到ListView的getBitmap

如果有人能帮助我。这是我的代码:

public void onCreate(Bundle savedInstanceState){ 
super.onCreate(savedInstanceState); 

    DatabaseHandler helper = new DatabaseHandler(this); 
    database = helper.getWritableDatabase(); 
    Cursor data = database.query("amigos", fields, null, null, null, null, null); 
    data.moveToFirst(); 
    dataSource = new SimpleCursorAdapter(this, R.layout.row_def, data, fields, new int[] {R.id.Nombre_Amigo, R.id.profile_picture}); 

    Log.d("ListarContactos","Oncreate....data: "+data.toString()); 

     while (!data.isAfterLast()){  
      byte[] bb = data.getBlob(data.getColumnIndex(DatabaseHandler.KEY_PIC_SQ)); 
      Log.d("ListarContactos","Oncreate....bb: "+bb.toString()); 
      ByteArrayInputStream inputStream = new ByteArrayInputStream(bb); 
      Log.d("ListarContactos","Oncreate....inputStream: "+inputStream.toString()); 
      Bitmap bitmap = BitmapFactory.decodeByteArray(bb, 0, bb.length); 
      Log.d("ListarContactos","Oncreate....Bitmap: "+bitmap.toString()); 
      ImageView image = (ImageView) findViewById(R.id.profile_picture); 
      image.setImageBitmap(bitmap); 
      Log.d("ListarContactos","Oncreate....image: "+image.toString()); 
     } 

    Log.d("ListarContactos","Oncreate....datasource: "+dataSource.toString());  
    setListAdapter(dataSource); 
    Log.d("ListarContactos","Oncreate....despres del ListAdapter");  
    helper.close(); 
    ListView view = getListView(); 
} 
+0

您正在模拟器或设备上测试应用程序? – 2012-08-17 11:36:50

+1

必须调用的setContentView(R.layout.yourlayout)尝试之前,使用findViewById()方法,在你的情况下,你(图)为空,,,即应该通过在ImageView的ID使用R.id.profile_picture中的setContentView布局模拟器上的 – 2012-08-17 11:39:08

+0

,这很重要?在我想在设备上测试之后。 – user1459089 2012-08-17 12:11:13

回答

0

它抛出nullPointerException因为方法BitmapFactory.decodeByteArray(bb, 0, bb.length)返回NULL为位图。