Java Android:不能从静态上下文中引用非静态方法。使困惑?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28044873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 05:30:56  来源:igfitidea点击:

Android: Non-static method cannot be referenced from static context. Confused?

javaandroid

提问by 343N

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

我对 Java 和一般编程非常陌生。我为一个基本程序编写了这个来添加用户输入的 2 个数字并将它们相加并显示在输出框中,但是我得到“非静态方法 'setText(java.lang.CharSequence)' 不能被引用从静态上下文”,但我不知道静态的东西是什么

    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.

对不起,如果我非常无能,但我搜索了,我真的不明白如何解决它。谢谢。

采纳答案by Stultuske

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).

您有一个名为 output1 的实例,但您尝试通过类的名称(这是一种静态方法)调用您的方法“setText”。

Change your lines

改变你的线条

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

to

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

回答by M D

Try this

尝试这个

output1.setText(result);

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

你不能setText()直接使用EditText. 为此,您将创建一个对象EditTextsetText()在其上使用。像这样

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

回答by Lawrence Wong

Change

改变

EditText.setText(result);

to

output1.setText(result);

回答by raj

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

当您已经将 EditText 的对象定义为 output1 时,现在您必须使用对象名称

use

 output1.setText(result);

inplaceof

代替

EditText.setText(result);

回答by mohit singh

Change Last Line to

将最后一行更改为

output1.setText(result);

回答by curiousType

this worked for me ty! :)

这对我有用!:)

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

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

        setContentView(output1);

回答by Jeff

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!

当我从实例名称或忘记实例化中删除前导字母(在本例中为“m”)时,发生了这种情况,产生了错误。因此,您可能不会立即看到这一点,而是将头撞在桌子上一段时间,这就是我刚刚所做的,使我提交了这个答案!