简体   繁体   English

类型无法从类型View静态引用非静态方法setVisibility(int)

[英]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.

相关问题 无法从类型View中静态引用非静态方法findViewById(int) - Cannot make a static reference to the non-static method findViewById(int) from the type View 无法解析webView且无法从Activity类型静态引用非静态方法findViewById(int) - webView cannot be resolved & cannot make a static reference to the non-static method findViewById(int) from the type Activity 无法从类型处理程序对非静态方法 sendEmptyMessage(int) 进行 static 引用 - Cannot make a static reference to the non-static method sendEmptyMessage(int) from the type Handler 无法从View MainActivity.java类型静态引用非静态方法invalidate() - Cannot make a static reference to the non-static method invalidate() from the type View MainActivity.java 无法从类型BaseGameActivity静态引用非静态方法getApiClient() - Cannot make a static reference to the non-static method getApiClient() from the type BaseGameActivity 无法从Component类型对静态方法setBackground(Color)进行静态引用 - Cannot make a static reference to the non-static method setBackground(Color) from the type Component 无法从类型片段中静态引用非静态方法getactivity() - cannot make a static reference to the non-static method getactivity() from the type fragment 获取错误“无法对类型为Intent的非静态方法putExtra(String,String)进行静态引用” - Getting error “Cannot make a static reference to the non-static method putExtra(String, String) from the type Intent” 无法从 Ethereum 类型对非静态方法 ethGetBlockByNumber(DefaultBlockParameter, boolean) 进行 static 引用 - Cannot make a static reference to the non-static method ethGetBlockByNumber(DefaultBlockParameter, boolean) from the type Ethereum 错误:“无法从类型Activity中对非静态方法startActivity(Intent)进行静态引用” - Error: “Cannot make a static reference to the non-static method startActivity(Intent) from the type Activity”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM