[英]Type Cannot make a static reference to the non-static method setVisibility(int) from the type View
I keep getting the following error in Eclipse: 我在Eclipse中不断收到以下错误:
Type Cannot make a static reference to the non-static method setVisibility(int) from the type View
类型无法从类型View静态引用非静态方法setVisibility(int)
My code is: 我的代码是:
package com.example.testing;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void onLoveButtonClicked(View view) {
TextView haikuTextView = (TextView) findViewById(R.id.haikuTextView);
TextView.setVisibility(View.VISIBLE); //error here
}
}
I'm only a beginner at java so I don't know what's causing that problem. 我只是Java的初学者,所以我不知道是什么导致了这个问题。 I have googled the error but I don't get anything that helps me.
我已经搜索了错误,但没有得到任何帮助。
You need to use: 您需要使用:
haikuTextView.setVisibility(View.VISIBLE);
TextView
is the class and haikuTextView
is your variable. TextView
是类, haikuTextView
是您的变量。 You cannot change the visibility of an entire class. 您不能更改整个班级的可见性。 But you can change the visibility of your variable.
但是您可以更改变量的可见性。
When you wrote: 当你写:
<TextView
android:id="@+id/haikuTextView"
... />
you created one instance of the TextView
class. 您创建了一个
TextView
类的实例 。 You can create a lot of instances of the TextView
class, but when you want to change some feature about a particular in one instance you have to specify which TextView
you want to change. 您可以创建许多
TextView
类的实例,但是当您要在一个实例中更改某个特定特性的某些功能时,必须指定要更改的TextView
。
When you wrote TextView.setVisibility()
you tried to change every TextView
. 当您编写
TextView.setVisibility()
您尝试更改每个 TextView
。 Now the TextView
class doesn't have a method setVisibility()
to change every TextView
, but it does have setVisibilty()
to change one instance. 现在,
TextView
类没有方法setVisibility()
来更改每个TextView
,但是它确实具有setVisibilty()
来更改一个实例。
So 所以
When you try to access every TextView
with TextView.setVisibility()
this is a "static reference" but, like I said, there is no method to call setVisibility()
every TextView
. 当您尝试使用
TextView.setVisibility()
访问每个TextView
,这是一个“静态引用”,但是,就像我说的那样,没有方法可以对每个 TextView
调用setVisibility()
。
If you use haikuTextView.setVisibilty()
to change the visibility of one instance, this will work because this is "non-static method" exists. 如果您使用
haikuTextView.setVisibilty()
更改一个实例的可见性,则将起作用,因为存在“非静态方法”。
To expand on Sam's answer: 扩展山姆的答案:
TextView is a class; TextView是一个类; it's also a subclass of the View class.
它也是View类的子类。 When you refer to static members of a class, you do "classname.method(...)" or "classname.field";
当引用一个类的静态成员时,执行“ classname.method(...)”或“ classname.field”; when you say "TextView.setVisibility", you're making a "static reference" to the method "setVisibility" in the "View" class, which is inherited by the TextView class.
当您说“ TextView.setVisibility”时,您是对“ View”类中的“ setVisibility”方法进行“静态引用”,该方法由TextView类继承。 Thus, the error message: Cannot make a static reference to the non-static method setVisibility(int) from the type View.
因此,错误消息:无法从视图类型静态引用非静态方法setVisibility(int)。 Because the setVisibility method isn't a static method;
因为setVisibility方法不是静态方法; it's an ordinary method.
这是一个普通的方法。
Ordinary methods have to be referred to relative to a specific instance of the class. 必须相对于类的特定实例引用普通方法。 Since haikuTextView is an instance of TextView;
由于haikuTextView是TextView的实例; you can say:
你可以说:
haikuTextView.setVisibility(View.VISIBLE);
Is that clear? 明白了吗?
It should be: 它应该是:
textView.setVisibility(View.VISIBLE);//A small t instead of a capital T.
Best of Luck 祝你好运
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.