尝试从SQLite数据库加载图像时,应用程序崩溃

时间:2017-06-13 17:52:06

标签: android sqlite android-sqlite numberformatexception

在我的应用程序中,我有一些EditText用户可以填写并点击保存,它将保存这些详细信息。我还有一个按钮,打开一个空白画布供用户登录(数字​​签名)。然后,当用户选择保存时,图像显示在ImageView中,最后用户可以再次点击保存以保存带有图像的表单。所有这一切都有效,但只要用户选择保存以保存表单,应用程序就会崩溃并出现以下错误:

java.lang.NumberFormatException: Invalid int: "android.widget.ImageView$ImageViewBitmapDrawable@efa14f0"
at java.lang.Integer.invalidInt(Integer.java:138)
at java.lang.Integer.parse(Integer.java:410)
at java.lang.Integer.parseInt(Integer.java:367)
at java.lang.Integer.parseInt(Integer.java:334)
at com.pta.sign.form.CreateFormActivity.persistPerson(CreateFormActivity.java:413)
at com.pta.sign.form.CreateFormActivity.onClick(CreateFormActivity.java:245)
at android.view.View.performClick(View.java:5697)
at android.widget.TextView.performClick(TextView.java:10814)
at android.view.View$PerformClick.run(View.java:22526)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

这是我使用的代码: 的 FormDBHelper.class

public static final String USER_SIGNATURE = "signature";
....
USER_SIGNATURE + " BLOB, " +
....
public boolean insertPerson(String customername,
                            ......
                            byte customersign, int 
                            jobnumber) {

 SQLiteDatabase db = this.getWritableDatabase();
 ContentValues contentValues = new ContentValues();

 contentValues.put(NAME_CUSTOMER, customername);
 ....
 contentValues.put(USER_SIGNATURE, customersign);
 contentValues.put(USER_JOB_NUMBER, jobnumber);

    db.insert(PERSON_TABLE_NAME, null, contentValues);
    return true;
}

public boolean updatePerson(Integer id,
                            String customername,
                            ....
                            byte customersign,

                            int jobnumber) {

    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(NAME_CUSTOMER, customername);
    ....
    contentValues.put(USER_SIGNATURE, customersign);
    contentValues.put(PERSON_JOB_NUMBER, jobnumber);

    db.update(PERSON_TABLE_NAME, contentValues, PERSON_COLUMN_ID + " = ? ", new String[]{Integer.toString(id)});
    return true;
}

然后在我的 CreateFormActivity.class

private FormDBHelper dbHelper;
EditText nameCustomer;
....
ImageView signatureImage;
Button saveButton;
LinearLayout buttonLayout;
Button editButton, deleteButton;
int personID;

.....

nameCustomer = (EditText) findViewById(R.id.editCustName);
....
signatureImage = (ImageView) findViewById(R.id.custimagesig);

....

 dbHelper = new FormDBHelper(this);

    if (personID > 0) {
        saveButton.setVisibility(View.GONE);
        buttonLayout.setVisibility(View.VISIBLE);

        rs = dbHelper.getPerson(personID);
        rs.moveToFirst();
        String customerName = rs.getString(rs.getColumnIndex(FormDBHelper.NAME_CUSTOMER));
        ....
        byte[] custsign = rs.getBlob(rs.getColumnIndex(FormDBHelper.USER_SIGNATURE));

 int personAge = rs.getInt(rs.getColumnIndex(FormDBHelper.PERSON_JOB_NUMBER));
        if (!rs.isClosed()) {
            rs.close();
        }

        nameCustomer.setText(customerName);
        nameCustomer.setFocusable(false);
        nameCustomer.setClickable(false);

        ....

        signatureImage.setImageBitmap(bitmap);
        signatureImage.setFocusable(false);
        signatureImage.setClickable(false);

  .....

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.saveButton:
            persistPerson(); **This is where is crashes (245)**
            return;
        case R.id.editButton:
            saveButton.setVisibility(View.VISIBLE);
            buttonLayout.setVisibility(View.GONE);

            nameCustomer.setEnabled(true);
            nameCustomer.setFocusableInTouchMode(true);
            nameCustomer.setClickable(true);

            ....

            signatureImage.setEnabled(false);
            signatureImage.setFocusableInTouchMode(true);
            signatureImage.setClickable(false);

   ......

 public void persistPerson() {
    if (personID > 0) {
        if (dbHelper.updatePerson(
  personID, 
  nameCustomer.getText().toString(),
  ....
  (byte) Integer.parseInt(signImage.getDrawable().toString()),
  ....
  Integer.parseInt(jobEditText.getText().toString()))) {
            Toast.makeText(getApplicationContext(), "Form Update Successful", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(getApplicationContext(), FragmentJob.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        } else {
            Toast.makeText(getApplicationContext(), "Form Update Failed", Toast.LENGTH_SHORT).show();
        }
    } else {
        if (dbHelper.insertPerson(
 nameCustomer.getText().toString(),
 ....
 (byte) Integer.parseInt(signImage.getDrawable().toString()), **this is where it crashes (413)**

                Integer.parseInt(jobEditText.getText().toString()))) {
            Toast.makeText(getApplicationContext(), "Form Inserted", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(getApplicationContext(), "Could not Insert Form", Toast.LENGTH_SHORT).show();
        }
        Intent intent = new Intent(getApplicationContext(), FragmentFormJob.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }
}

这是我的代码(在同一个类中),用户点击按钮创建他们的签名:

Button.OnClickListener onButtonClick = new Button.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent i = new Intent(CreateFormActivity.this, CaptureUserSig.class);
        startActivityForResult(i, 0);
    }
};

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    if (resultCode == 1) {
        Bitmap b = BitmapFactory.decodeByteArray(
                data.getByteArrayExtra("byteArray"), 0,
                data.getByteArrayExtra("byteArray").length);
        signatureImage.setImageBitmap(Bitmap.createScaledBitmap(b, 512, 512, false));
    }
}

}

AFAIK这应该有效,除非我在某个地方犯了一个错误。但对于我的生活,我似乎无法弄明白。

0 个答案:

没有答案
相关问题