I keep getting the following error in Eclipse:
Type Cannot make a static reference to the non-static method setVisibility(int) from the type View
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. I have googled the error but I don’t get anything that helps me.
You need to use:
TextViewis the class andhaikuTextViewis your variable. You cannot change the visibility of an entire class. But you can change the visibility of your variable.When you wrote:
you created one instance of the
TextViewclass. You can create a lot of instances of theTextViewclass, but when you want to change some feature about a particular in one instance you have to specify whichTextViewyou want to change.When you wrote
TextView.setVisibility()you tried to change everyTextView. Now theTextViewclass doesn’t have a methodsetVisibility()to change everyTextView, but it does havesetVisibilty()to change one instance.So
When you try to access every
TextViewwithTextView.setVisibility()this is a “static reference” but, like I said, there is no method to callsetVisibility()everyTextView.If you use
haikuTextView.setVisibilty()to change the visibility of one instance, this will work because this is “non-static method” exists.