尝试从ImageView获取图像时出现空指针异常

时间:2013-01-18 00:03:16

标签: android nullpointerexception android-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)

2 个答案:

答案 0 :(得分:2)

如果你直接传递Bitmap而不是imgHolder.getDrawableCache()它会更好,它应该可以工作,

BitmapDrawable drawable = (BitmapDrawable) imgHolder.getDrawable();
Bitmap bitmap = drawable.getBitmap();

i = new Intent(v.getContext(), ViewActivity.class);
//Add contact to database
dbAdapter.addContact(nameBox.getText().toString(), numberBox.getText().toString(), emailBox.getText().toString(), bitmap);
startActivity(i);

答案 1 :(得分:0)

尝试在调用imgHolder.getDrawingCache();

之前调用相应布局上的layout(int, int, int, int)

在这里看到:Android 2.1 View's getDrawingCache() method always returns null