2012-04-13 169 views
0

當我進入此頁面並單擊後退按鈕我得到一個力量關閉,因爲它強制關閉我的條形碼掃描結果不顯示。如何停止應用程序部隊的關閉?強制關閉時按下按鈕

private EditText mTitleText; 
private EditText mBodyText; 
private EditText mquantityint; 
private EditText mbarcodeint; 
private Long mRowId; 
private NotesDbAdapter mDbHelper; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    mDbHelper = new NotesDbAdapter(this); 
    mDbHelper.open(); 

    setContentView(R.layout.note_edit); 

    mTitleText = (EditText) findViewById(R.id.title); 
    mBodyText = (EditText) findViewById(R.id.body); 
    mquantityint = (EditText) findViewById(R.id.quantity); 
    mbarcodeint = (EditText) findViewById(R.id.barcode); 

    Button barcode2 = (Button) findViewById(R.id.sbarcode); 
    barcode2.setOnClickListener(new OnClickListener() { 
     public void onClick(View arg0) 
     { 
      Intent intent = new Intent("com.google.zxing.client.android.SCAN"); 
      intent.putExtra("SCAN_MODE", "PRODUCT_MODE"); 
      intent.putExtra("SAVE_HISTORY", false);//this stops saving ur barcode in barcode scanner app's history 
      startActivityForResult(intent, 0); 
     } 
    }); 

    Button confirmButton = (Button) findViewById(R.id.confirm); 

    mRowId = (savedInstanceState == null) ? null : 
     (Long) savedInstanceState.getSerializable(NotesDbAdapter.KEY_ROWID); 
    if (mRowId == null) { 
     Bundle extras = getIntent().getExtras(); 
     mRowId = extras != null ? extras.getLong(NotesDbAdapter.KEY_ROWID) 
           : null; 
    } 

    populateFields(); 

    confirmButton.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 
      setResult(RESULT_OK); 
      finish(); 
     } 
    }); 
} 

private void populateFields() { 
    if (mRowId != null) { 
     Cursor note = mDbHelper.fetchNote(mRowId); 
     startManagingCursor(note); 
     mTitleText.setText(note.getString(
       note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE))); 
     mBodyText.setText(note.getString(
       note.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY))); 
     mquantityint.setText(note.getString(
       note.getColumnIndexOrThrow(NotesDbAdapter.KEY_QUANTITY))); 
     mbarcodeint.setText(note.getString(
       note.getColumnIndexOrThrow(NotesDbAdapter.KEY_BARCODE))); 
    } 
} 

@Override 
protected void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    saveState(); 
    outState.putSerializable(NotesDbAdapter.KEY_ROWID, mRowId); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    saveState(); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    populateFields(); 
} 

private void saveState() { 
    String title = mTitleText.getText().toString(); 
    String body = mBodyText.getText().toString(); 
    int quantity =Integer.parseInt(mquantityint.getText().toString()); 
    int barcode =Integer.parseInt(mbarcodeint.getText().toString()); 
    if (mRowId == null) { 
     long id = mDbHelper.createNote(title, body, quantity, barcode); 
     if (id > 0) { 
      mRowId = id; 
     } 
    } else { 
     mDbHelper.updateNote(mRowId, title, body, quantity, barcode); 
    } 
} 

public void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    String contents =null; 
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == 0) 
    { 
     if (resultCode == RESULT_OK) 
     { 
      contents = data.getStringExtra("SCAN_RESULT"); 
      // String format = data.getStringExtra("SCAN_RESULT_FORMAT"); 
      //Variable set for QR scan result 
      EditText sbarcodeno = (EditText) findViewById(R.id.barcodeno); 
      //Variable set to display custom string above result 
      EditText Result =(EditText) findViewById(R.id.sbarcode); 
      //Displays actual 
      sbarcodeno.setText(contents); 
      //Edit string here  
      Result.setText(""); 
      // TO DO - Modify code so the result launches in another activity. 
     } 
     else if (resultCode == RESULT_CANCELED) 
     { 
      // Handle cancel 
     } 
    } 
} 
@Override 
public void onBackPressed() { 
    this.finish(); 
    return; 
} 
} 

的logcat -

04-13 01:24:47.355: E/AndroidRuntime(2575): FATAL EXCEPTION: main 
04-13 01:24:47.355: E/AndroidRuntime(2575): java.lang.RuntimeException: Unable to pause activity {bhavik.smart.shopping/bhavik.smart.shopping.NoteEdit}: java.lang.NumberFormatException: Invalid int: "" 
04-13 01:24:47.355: E/AndroidRuntime(2575):  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2706) 
04-13 01:24:47.355: E/AndroidRuntime(2575):  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2662) 
04-13 01:24:47.355: E/AndroidRuntime(2575):  at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2640) 
04-13 01:24:47.355: E/AndroidRuntime(2575):  at android.app.ActivityThread.access$800(ActivityThread.java:123) 
04-13 01:24:47.355: E/AndroidRuntime(2575):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158) 
04-13 01:24:47.355: E/AndroidRuntime(2575):  at android.os.Handler.dispatchMessage(Handler.java:99) 
04-13 01:24:47.355: E/AndroidRuntime(2575):  at android.os.Looper.loop(Looper.java:137) 
04-13 01:24:47.355: E/AndroidRuntime(2575):  at android.app.ActivityThread.main(ActivityThread.java:4424) 
04-13 01:24:47.355: E/AndroidRuntime(2575):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-13 01:24:47.355: E/AndroidRuntime(2575):  at java.lang.reflect.Method.invoke(Method.java:511) 
04-13 01:24:47.355: E/AndroidRuntime(2575):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787) 
04-13 01:24:47.355: E/AndroidRuntime(2575):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554) 
04-13 01:24:47.355: E/AndroidRuntime(2575):  at dalvik.system.NativeStart.main(Native Method) 
04-13 01:24:47.355: E/AndroidRuntime(2575): Caused by: java.lang.NumberFormatException: Invalid int: "" 
04-13 01:24:47.355: E/AndroidRuntime(2575):  at java.lang.Integer.invalidInt(Integer.java:138) 
04-13 01:24:47.355: E/AndroidRuntime(2575):  at java.lang.Integer.parseInt(Integer.java:359) 
04-13 01:24:47.355: E/AndroidRuntime(2575):  at java.lang.Integer.parseInt(Integer.java:332) 
04-13 01:24:47.355: E/AndroidRuntime(2575):  at bhavik.smart.shopping.NoteEdit.saveState(NoteEdit.java:138) 
04-13 01:24:47.355: E/AndroidRuntime(2575):  at bhavik.smart.shopping.NoteEdit.onPause(NoteEdit.java:126) 
04-13 01:24:47.355: E/AndroidRuntime(2575):  at android.app.Activity.performPause(Activity.java:4563) 
04-13 01:24:47.355: E/AndroidRuntime(2575):  at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1195) 
04-13 01:24:47.355: E/AndroidRuntime(2575):  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2693) 
04-13 01:24:47.355: E/AndroidRuntime(2575):  ... 12 more 

回答

2

你是因爲線

int quantity =Integer.parseInt(mquantityint.getText().toString()); 
int barcode =Integer.parseInt(mbarcodeint.getText().toString()); 

在saveState和()的接近得到一個力。當您按下後退按鈕時,其中一個TextView是空的。

注意這條線在錯誤日誌

java.lang.NumberFormatException: Invalid int: "" 

當你在一個無效的字符串運行parseInt函數這被拋出。在這種情況下,它告訴你該字符串是空的。你需要在運行parseInt之前檢查字符串是否爲空,或者在方法周圍進行try catch。

祝你好運。

0
Result.setText(""); 

從你的代碼中你解析到字符串,所以得到numberformat異常。 只檢查簡單condtions轉換之前爲整數像下面::

if(Result.getText()!="" ||Result.getText()!=null) 
int barcode =Integer.parseInt(mbarcodeint.getText().toString()); 
else 
int barcode =0; 

上面可以去除其他部分爲[呃你要求