-2

``Hi, i have a calculator application and on the screen there are buttons for number and add, multiplication, clean, substruction and division buttons. But when I open and click buttons they couldnt make change on editText. When i write number on keybord, it edited. How can i fix it?

Here is full of my java code

private EditText screen; //textbox screen
private float numberBf; // save screen before pressing button operation
private String operation;
private ButtonClickListener btnClick;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    screen = (EditText) findViewById(editText);

    int idList[] = {R.id.button0,R.id.button,R.id.button2,R.id.button3,R.id.button4,
            R.id.button5,R.id.button6,R.id.button7,R.id.button8,R.id.button9, R.id.buttonAdd,R.id.buttonMul,
            R.id.buttonC, R.id.buttonDiv,R.id.buttonDot,R.id.buttonEq,R.id.buttonSub};

    for(int id:idList){
        View v = (View) findViewById(id);
        v.setOnClickListener(btnClick);
    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
//new class
private class ButtonClickListener implements View.OnClickListener {
    public void onClick(View view){
        switch (view.getId()){
            case R.id.buttonC: //clear button
                screen.setText("0");
                numberBf = 0;
                operation = "";
                break;
            //adding function
            case R.id.buttonAdd:
                mMath("+");
                break;
            case R.id.buttonSub:
                mMath("-");
                break;
            case R.id.buttonMul:
                mMath("*");
                break;
            case R.id.buttonDiv:
                mMath("/");
                break;
            case R.id.buttonEq:
                mResult();
                break;
            default:
                String num = ((Button) view).getText().toString();
                getKeyboard(num);
                break;
        }
    }
}

public void mMath(String s){
    numberBf = Float.parseFloat(screen.getText().toString()); //save the screen
    operation = s; //save operation
    screen.setText("0"); //clear screen
}
public void mResult(){
    float numberFl = Float.parseFloat(screen.getText().toString());
    float result = 0;
    if(operation.equals("+"))
        result = numberFl + numberBf;
    if (operation.equals("-"))
        result =  numberBf - numberFl;
    if(operation.equals("*"))
        result = numberFl * numberBf;
    if (operation.equals("/"))
        result = numberBf / numberFl;

    screen.setText(String.valueOf(result));
}
public void getKeyboard(String str){
    String scrCurrent = screen.getText().toString();
    if (scrCurrent.equals("0"))
        scrCurrent = "";
    scrCurrent = str;
    screen.setText(str);
}

}

and some part of my activity_main xml code, number 9 button and text place is

activity_main :

<EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/editText" android:text="0" android:layout_below="@+id/button0" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:layout_marginTop="57dp" android:gravity="right" android:editable="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="9" android:id="@+id/button9" android:layout_below="@+id/button5" android:layout_toRightOf="@+id/button5" android:textStyle="bold" />

please help me thank you

Pragnesh Ghoda シ
  • 8,318
  • 3
  • 25
  • 40
Irem Herguner
  • 47
  • 1
  • 8

3 Answers3

1

This should make it work:

screen = (EditText) findViewById(R.id.editText); 
dan983
  • 454
  • 2
  • 5
  • 19
1
screen = (EditText) findViewById(editText);

The parameter editText is not a valid Resource Identifier. In XML you should have

<EditText ...
   android:id="@+id/thetextid">

And then in your code you should have

screen = (EditText) findViewById(R.id.thetextid);
mapodev
  • 988
  • 8
  • 14
  • FWIW, it works if OP had imported `pkgname.R.id` and apparently it was imported since the unspecified error was runtime and not compile-time. – laalto Jul 02 '14 at 09:24
0

You haven't instantiated the button click listener. Add

btnClick = new ButtonClickListener();

before any of the setOnClickListener(btnClick) calls.

laalto
  • 150,114
  • 66
  • 286
  • 303