12

I am extremely extremely new to Java and programming in general. I wrote this for a basic program to add 2 numbers input by the user and add them up and display them in the output box, however I'm getting Non-static method 'setText(java.lang.CharSequence)' cannot be referenced from a static context, but I don't know what the static thing is

private void onClick(View v) {
    EditText input1 = (EditText) findViewById(R.id.input1);
    double calc1 =  Double.parseDouble(String.valueOf(input1));
    EditText input2 = (EditText) findViewById(R.id.input2);
    double calc2 = Double.parseDouble(String.valueOf(input2));
    double total = calc1 + calc2;
    String result = Double.toString(total);
    EditText output1 = (EditText) findViewById(R.id.output);
    EditText.setText(result);
}

The line giving the error:

EditText.setText(result);

Sorry if I'm being extremely incompetent but I searched and I couldn't really understand how to fix it. Thanks.

Lloyd Dominic
  • 778
  • 8
  • 25
343N
  • 296
  • 2
  • 4
  • 19

7 Answers7

35

In a static context, you don't have an object (instance of the class), but the instance variables and methods depend on them.

You have an instance, called output1, but you try to call your method 'setText' through the class's name (which is a static approach).

Change your lines

EditText output1 = (EditText)findViewById(R.id.output);
    EditText.setText(result);

to

EditText output1 = (EditText)findViewById(R.id.output);
    output1.setText(result);
Stultuske
  • 9,296
  • 1
  • 25
  • 37
  • This is also because you define EditText output1; and you shrink this line with "=" to keep going coding. So, output1.setText(); is a clear usage. – Bay Sep 26 '19 at 09:57
  • @Bay I have no idea what you just tried to say, to be honest. – Stultuske Sep 26 '19 at 10:18
  • When you type this: EditText output1 = (EditText)findViewById(R.id.output); Android Studio says "split into assignment and declaration" this is optional I mean. – Bay Sep 26 '19 at 12:55
3

Change

EditText.setText(result);

to

output1.setText(result);
Lawrence Wong
  • 1,129
  • 4
  • 24
  • 40
1

Try this

output1.setText(result);

You can't used setText() directly to EditText. For that you'll create a object of EditText and used setText() on it. like so

 EditText output1 = (EditText)findViewById(R.id.output);
 output1.setText(result);
M D
  • 47,665
  • 9
  • 93
  • 114
1

when you already defined object of EditText as output1 then now you have to use object name

use

 output1.setText(result);

inplaceof

EditText.setText(result);
raj
  • 2,088
  • 14
  • 23
1

Change Last Line to

output1.setText(result);
mohit singh
  • 149
  • 1
  • 5
0

this worked for me ty! :)

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TextView output1 = new TextView(this);
        output1.setText("Wow!");

        setContentView(output1);
0

Also, this could be due to trying to access a class' method directly rather than from instantiating it, such as:

int returnVal = SomeViewModelClass.updateRecord(table, values, where);

This will result in the error: "Non-static method cannot be referenced from static context"

So, ensure that you have instantiated it appropriately, in this case like:

private SomeViewModelClass mSomeViewModelClass;
.
.
.
mSomeViewModelClass = ViewModelProviders.of(this).get(SomeViewModelClass.class);

I've had this happen, generated the error, when I deleted the leading letter, in this case "m", from the instance name or from forgetting to instantiate. So, you might not see this immediately and bang your head on the desk for a while, which is what I just did that brought me to submit this answer!

Jeff
  • 431
  • 4
  • 16