对于Object类型,方法add,pop,size,clear,get和iterator是未定义的

时间:2014-02-10 07:23:46

标签: android android-layout android-fragments

我在MainActivity类中遇到错误。在Method.add(),pop(),size(),clear(),get()和iterator()中出现的错误未定义为Object类型。

public class MainActivity extends Activity {
GridView mKeypadGrid;
KeyAdapter mKeypadAdapter;
private TextView userInputText;
private boolean resetInput;
private boolean hasFinalResult;
private String mDecimalSeperator;
private Object mInputStack;
private Object mOperationStack;
private double memoryValue;
private TextView mStackText;
private TextView memoryStatText;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


 mKeypadGrid = (GridView)findViewById(R.id.grdButtons);

 mKeypadAdapter = new KeyAdapter(this);


 mKeypadGrid.setAdapter(mKeypadAdapter);
 mKeypadAdapter.setOnButtonClickListener(new OnClickListener() {
     @Override
     public void onClick(View v) {
       Button btn = (Button) v;

       KeypadButton keypadButton = (KeypadButton) btn.getTag();


       ProcessKeypadInput(keypadButton);
      }});
 mKeypadGrid.setOnItemClickListener(new OnItemClickListener() {
     public void onItemClick(AdapterView<?> parent, View v,int position, long id) {

     }
 });
}

 public void ProcessKeypadInput(KeypadButton keypadButton) {

        String text = keypadButton.getText().toString();
        String currentInput = userInputText.getText().toString();

        int currentInputLen = currentInput.length();
        String evalResult = null;
        double userInputValue = Double.NaN;

        switch (keypadButton) {
        case BACKSPACE: 
            if (resetInput)
                return;

            int endIndex = currentInputLen - 1;


            if (endIndex < 1) {
                userInputText.setText("0");
            }
            else {
                userInputText.setText(currentInput.subSequence(0, endIndex));
            }
            break;
        case SIGN:  
            if (currentInputLen > 0 && currentInput != "0") {

                if (currentInput.charAt(0) == '-') {
                    userInputText.setText(currentInput.subSequence(1,
                            currentInputLen));
                }

                else {
                    userInputText.setText("-" + currentInput.toString());
                }
            }
            break;
        case CE: 
            userInputText.setText("0");
            break;
        case C:
            userInputText.setText("0");
            clearStacks();
            break;
        case DECIMAL_SEP: 
            if (hasFinalResult || resetInput) {
                userInputText.setText("0" + mDecimalSeperator);
                hasFinalResult = false;
                resetInput = false;
            } else if (currentInput.contains("."))
                return;
            else
                userInputText.append(mDecimalSeperator);
            break;
        case DIV:
        case PLUS:
        case MINUS:
        case MULTIPLY:
            if (resetInput) {
                mInputStack.pop();  //The method pop() is undefined for the type Object
                mOperationStack.pop();  //The method pop() is undefined for the type Object
            } else {
                if (currentInput.charAt(0) == '-') {
                    mInputStack.add("(" + currentInput + ")");  //The method add(String) is undefined for the type Objec
                } else {
                    mInputStack.add(currentInput);  //The method add(String) is undefined for the type Object
                }
                mOperationStack.add(currentInput);
            }

            mInputStack.add(text);
            mOperationStack.add(text);

            dumpInputStack();
            evalResult = evaluateResult(false);
            if (evalResult != null)
                userInputText.setText(evalResult);

            resetInput = true;
            break;
        case CALCULATE:
            if (mOperationStack.size() == 0)
                break;

            mOperationStack.add(currentInput);
            evalResult = evaluateResult(true);
            if (evalResult != null) {
                clearStacks();
                userInputText.setText(evalResult);
                resetInput = false;
                hasFinalResult = true;
            }
            break;
        case M_ADD: 
            userInputValue = tryParseUserInput();
            if (Double.isNaN(userInputValue))
                return;
            if (Double.isNaN(memoryValue))
                memoryValue = 0;
            memoryValue += userInputValue;
            displayMemoryStat();

            hasFinalResult = true;

            break;
        case M_REMOVE: 
            userInputValue = tryParseUserInput();
            if (Double.isNaN(userInputValue))
                return;
            if (Double.isNaN(memoryValue))
                memoryValue = 0;
            memoryValue -= userInputValue;
            displayMemoryStat();
            hasFinalResult = true;
            break;
        case MC: 
            memoryValue = Double.NaN;
            displayMemoryStat();
            break;
        case MR:
            if (Double.isNaN(memoryValue))
                return;
            userInputText.setText(doubleToString(memoryValue));
            displayMemoryStat();
            break;
        case MS:
            userInputValue = tryParseUserInput();
            if (Double.isNaN(userInputValue))
                return;
            memoryValue = userInputValue;
            displayMemoryStat();
            hasFinalResult = true;
            break;
        default:
            if (Character.isDigit(text.charAt(0))) {
                if (currentInput.equals("0") || resetInput || hasFinalResult) {
                    userInputText.setText(text);
                    resetInput = false;
                    hasFinalResult = false;
                } else {
                    userInputText.append(text);
                    resetInput = false;
                }

            }
            break;

        }

    }

    public void clearStacks() {
        mInputStack.clear();
        mOperationStack.clear();  //The method clear() is undefined for the type Object
        mStackText.setText("");
    }

    public void dumpInputStack() {
        Iterator<String> it = mInputStack.iterator();  //The method iterator() is undefined for the type Object
        StringBuilder sb = new StringBuilder();

        while (it.hasNext()) {
            CharSequence iValue = it.next();
            sb.append(iValue);

        }

        mStackText.setText(sb.toString());
    }

    public String evaluateResult(boolean requestedByUser) {
        if ((!requestedByUser && mOperationStack.size() != 4)  
                || (requestedByUser && mOperationStack.size() != 3))  //The method size() is undefined for the type Object
            return null;

        String left = mOperationStack.get(0);
        String operator = mOperationStack.get(1);  //The method get(int) is undefined for the type Object
        String right = mOperationStack.get(2);
        String tmp = null;
        if (!requestedByUser)
            tmp = mOperationStack.get(3);

        double leftVal = Double.parseDouble(left.toString());
        double rightVal = Double.parseDouble(right.toString());
        double result = Double.NaN;

        if (operator.equals(KeypadButton.DIV.getText())) {
            result = leftVal / rightVal;
        } else if (operator.equals(KeypadButton.MULTIPLY.getText())) {
            result = leftVal * rightVal;

        } else if (operator.equals(KeypadButton.PLUS.getText())) {
            result = leftVal + rightVal;
        } else if (operator.equals(KeypadButton.MINUS.getText())) {
            result = leftVal - rightVal;

        }

        String resultStr = doubleToString(result);
        if (resultStr == null)
            return null;

        mOperationStack.clear();
        if (!requestedByUser) {
            mOperationStack.add(resultStr);
            mOperationStack.add(tmp);
        }

        return resultStr;
    }

    public String doubleToString(double value) {
        if (Double.isNaN(value))
            return null;

        long longVal = (long) value;
        if (longVal == value)
            return Long.toString(longVal);
        else
            return Double.toString(value);

    }

    public double tryParseUserInput() {
        String inputStr = userInputText.getText().toString();
        double result = Double.NaN;
        try {
            result = Double.parseDouble(inputStr);

        } catch (NumberFormatException nfe) {}
        return result;

    }

    public void displayMemoryStat() {
        if (Double.isNaN(memoryValue)) {
            memoryStatText.setText("");
        } else {
            memoryStatText.setText("M = " + doubleToString(memoryValue));
        }
    }
 }

除了这些MainActivity Class,一切都会好的。任何人都知道如何解决这些错误。

1 个答案:

答案 0 :(得分:1)

声明变量mInputStackmOperationStack作为Stack的引用,而不是Object(当然StackObject,但您需要对于类型转换。)

所以不是这个

private Object mInputStack;
private Object mOperationStack;

写得像

private Stack mInputStack;
private Stack mOperationStack;

并且在使用之前不要忘记初始化它们。