我正試圖從窗體向設備上的本地數據庫提交詳細信息。我得到一個空指針,似乎在這部分代碼中發生。我有一種感覺,這可能與我試圖從ImageView中獲取圖像的方式有關。嘗試從ImageView中獲取圖像時發生空指針異常
任何想法?
addContactBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Add Contact", Toast.LENGTH_SHORT).show();
i = new Intent(v.getContext(), ViewActivity.class);
//Add contact to database
dbAdapter.addContact(nameBox.getText().toString(), numberBox.getText().toString(), emailBox.getText().toString(), imgHolder.getDrawingCache());
startActivity(i);
}
});
}
這是的addContact()方法:
public void addContact(String name, String number, String email, Bitmap img){
ContentValues values = new ContentValues();
ByteArrayOutputStream out = new ByteArrayOutputStream();
img.compress(Bitmap.CompressFormat.PNG, 100, out);
values.put(COLUMN_NAME, name);
values.put(COLUMN_NUMBER, number);
values.put(COLUMN_EMAIL, email);
values.put(COLUMN_IMG, out.toByteArray());
db.insert(TABLE_NAME, null, values);
Toast.makeText(context, "Contact Added Successfully!", Toast.LENGTH_SHORT).show();
}
這是日誌輸出:
01-17 23:57:16.939: E/AndroidRuntime(4582): FATAL EXCEPTION: main
01-17 23:57:16.939: E/AndroidRuntime(4582): java.lang.NullPointerException
01-17 23:57:16.939: E/AndroidRuntime(4582): at com.example.contactappca2.AddActivity$2.onClick(AddActivity.java:73)
01-17 23:57:16.939: E/AndroidRuntime(4582): at android.view.View.performClick(View.java:3549)
01-17 23:57:16.939: E/AndroidRuntime(4582): at android.view.View$PerformClick.run(View.java:14400)
01-17 23:57:16.939: E/AndroidRuntime(4582): at android.os.Handler.handleCallback(Handler.java:605)
01-17 23:57:16.939: E/AndroidRuntime(4582): at android.os.Handler.dispatchMessage(Handler.java:92)
01-17 23:57:16.939: E/AndroidRuntime(4582): at android.os.Looper.loop(Looper.java:154)
01-17 23:57:16.939: E/AndroidRuntime(4582): at android.app.ActivityThread.main(ActivityThread.java:4944)
01-17 23:57:16.939: E/AndroidRuntime(4582): at java.lang.reflect.Method.invokeNative(Native Method)
01-17 23:57:16.939: E/AndroidRuntime(4582): at java.lang.reflect.Method.invoke(Method.java:511)
01-17 23:57:16.939: E/AndroidRuntime(4582): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-17 23:57:16.939: E/AndroidRuntime(4582): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-17 23:57:16.939: E/AndroidRuntime(4582): at dalvik.system.NativeStart.main(Native Method)
什麼也正是在這條線'(AddActivity.java:73)'? – Ameen
如果您將線分成不同的線條,可能會讓您更好地瞭解您的哪些對象初始化不良。 – DigCamara
@Ameen 73行:'imgHolder.getDrawingCache();' – Javacadabra