分貝的android我是做一個應用程序在SQLite數據庫中插入數據和ii有一個空指針異常時記錄添加到數據庫空指針異常時嵌入照片中的Android
這個初學者代碼得到的ImageView的PIC,並將其轉換再到位圖以字節來保存 任何建議,以避免此異常 這裏是代碼
imageView1.setDrawingCacheEnabled(true);
imageView1.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
imageView1.layout(0, 0, imageView1.getMeasuredWidth(), imageView1.getMeasuredHeight());
imageView1.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(imageView1.getDrawingCache());
imageView1.setDrawingCacheEnabled(false);
save_picture=loginDataBaseAdapter.getBytes(b);
loginDataBaseAdapter.insertEntry(userName, password, save_picture);
Toast.makeText(getApplicationContext(), "Record created ", Toast.LENGTH_LONG).show();`}
有轉換位爲字節的方法
// convert from bitmap to byte array
public byte[] getBytes(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0, stream);
return stream.toByteArray();
}
並沒有插入方法
public void insertEntry(String userName,String password , byte[] photo )
{
ContentValues newValues = new ContentValues();
// Assign values for each row.
newValues.put("USERNAME", userName);
newValues.put("PASSWORD",password);
newValues.put("Photo", photo);
// Insert the row into your table
db.insert("LOGIN", null, newValues);
}
和存在的DB創建
static final String DATABASE_CREATE = "create table "+"LOGIN"+
"(" +"ID"+" integer primary key autoincrement,"+ "USERNAME text,PASSWORD text, Photo blob not null); ";
感謝
我們能看到錯誤輸出? –
可能重複[什麼是空指針異常,以及如何解決它?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do -i-fix-it) – njzk2
E/AndroidRuntime(15303):致命例外:main E/AndroidRuntime(15303):java.lang.NullPointerException E/AndroidRuntime(15303):\t at android.graphics.Bitmap.createBitmap( Bitmap.java:571) E/AndroidRuntime(15303):\t at com.techblogon.loginexample.SignUPActivity $ 1.onClick(SignUPActivity.java:91) E/AndroidRuntime(15303):\t at android.view.View.performClick (View.java:2532) 這裏是錯誤 – BOB91