0

我有一个 Android 应用程序,它简单地保存数据并将它们显示在列表视图中,与记事本教程非常相似。不幸的是,我的 Add 活动似乎已经停止工作。当我尝试添加数据并按确认时,它会重新加载活动(屏幕轻微闪烁),并且我输入到字段中的所有数据都会被清除。我已经确认它没有达到 onSaveInstanceState()。我的印象是这个方法是在完成()时自动调用的,就像我提到的那样,它一次可以工作。也许有人可以发现我在代码中引入了错误的地方?我将粘贴我认为是相关部分的内容:

 confirmButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {

            //Verify that fields are filled out
            String description = mDescriptionText.getText().toString();
            String amount = mAmountText.getText().toString();
            if(description.length() == 0 || amount.length() == 0) {
                if(description.length() == 0) {
                    Context context = getApplicationContext();
                    CharSequence text = "A description is required";
                    int duration = Toast.LENGTH_SHORT;
                    Toast toast = Toast.makeText(context, text, duration);
                    toast.show();
                }
                else {
                    Context context = getApplicationContext();
                    CharSequence text = "An amount is required";
                    int duration = Toast.LENGTH_SHORT;
                    Toast toast = Toast.makeText(context, text, duration);
                    toast.show();
                }

            }
            else {
                /* Call this to set the result that your activity will return to its caller */
                setResult(RESULT_OK);
                /* Call this when your activity is done and should be closed. The ActivityResult is propagated back to 
                whoever launched you via onActivityResult() */ 


                finish();
            }
        }


    @Override
protected void onSaveInstanceState(Bundle outState) {
    Log.i("expEdit","onSaveInstance State Reached");
    super.onSaveInstanceState(outState);
    saveState();
    outState.putSerializable(EZBudgetDbAdapter.KEY_ROWID, mRowId);
}


 private void saveState() {
    Log.i("expEdit","saveState Reached");
    String description = mDescriptionText.getText().toString();
    String amount = mAmountText.getText().toString();
    Double dAmount = 0.0;
    if(amount != "") {
        dAmount = Double.valueOf(amount).doubleValue();
    }

    if (mRowId == null) {
        long id = mExpDbHelper.createExpenditure(description, dAmount);
        if (id > 0) {
            mRowId = id;
        }

        if (mSaveDesc.isChecked()) {
            // Save the description to the CommonDesc table
            mCommDbHelper = new CommonDescDbAdapter(this);
            mCommDbHelper.open();
            mCommDbHelper.createCommonDesc(description);

        }

    } else {

        mExpDbHelper.updateExpenditure(mRowId, description, dAmount);

        if (mSaveDesc.isChecked()) {
            // Save the description to the CommonDesc table
            mCommDbHelper = new CommonDescDbAdapter(this);
            mCommDbHelper.open();
            mCommDbHelper.createCommonDesc(description);

        }


    }
}
4

1 回答 1

1

在finish() 之后不调用onSaveInstanceState()。请参阅以下onSaveInstanceState

而且您的代码不干净。永远不要白白复制代码。你应该使用

    Context context = getApplicationContext();
    CharSequence text;
    int duration = Toast.LENGTH_SHORT;

    if(description.length() == 0) {                    
        text = "A description is required";                 
    } else {                    
        text = "An amount is required";                    
    }
    Toast toast = Toast.makeText(context, text, duration);
    toast.show();
于 2012-10-19T18:48:28.400 回答