-4

I have this in my java code, when I try to compile my code I get an error. This happens when I try to get the value of the text in my textview into a var. I cannot understand this error because It works fine in the other method.

Why happens this and how can I fix it?

    public class MainActivity extends Activity {

        public EditText editText;
        TextView textView;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Toast.makeText(MainActivity.this, "onCreate", Toast.LENGTH_LONG).show();

            //setupMessageButton();

            editText = (EditText) findViewById(R.id.editText1);
            textView = (TextView)findViewById(R.id.tvIsConnected);


        }

        public void btnDisplayMessage(View view){

            //HERE WORKS FINE
            String missatge = editText.getText().toString();

        }

    public static String POST(String url){
        InputStream inputStream = null;
        String result = "";
        //HERE CRASHES
        String missatge = "red"//editText.getText().toString(); GIVES ERROR
        String usuario = "foo";

............

WHY?

EDIT: Thanks for downvote my question, Yes, I searched for another similar questions and I've already tried with static method...then don't crash but the content of the var is null and don't work at all.

Cœur
  • 37,241
  • 25
  • 195
  • 267
BugFixer
  • 293
  • 1
  • 4
  • 15
  • 4
    What did your extensive search bring up? – Sotirios Delimanolis Apr 23 '14 at 16:35
  • 1
    Make `post` method non static. as you can't access non static variable inside static method. – bNd Apr 23 '14 at 16:36
  • 1
    "_I cannot understand this error_" You can't? Or haven't tried? – takendarkk Apr 23 '14 at 16:36
  • 1
    I just clicked "close as duplicate" and picked the first result. That's how easy it was to find a duplicate of this question. – djechlin Apr 23 '14 at 16:37
  • should we not answer this question then? – Jake B Apr 23 '14 at 16:38
  • you need to include any error messages as a part of your question. – Richard Chambers Apr 23 '14 at 16:39
  • 1
    @JakeB IMO (which could be wrong) if the question is like this where it involves a basic java error that has been gone over tons of times already on this site, by answering you would just be promoting people to keep asking theses questions. – takendarkk Apr 23 '14 at 16:45
  • @Takendarkk good point. I think the little box that pops up when you're typing your question that contains possible duplicate questions is pretty self explanatory. It is probably better to have this question just link to the duplicate real question with all of the answers there. – Jake B Apr 23 '14 at 17:10
  • Also, whenever you view a question you can look to the right and see possible duplicates or related questions in the margin. – takendarkk Apr 23 '14 at 17:18

1 Answers1

0

You can not access a variable non static from a static method...

Solutions:

  1. Make editText static
  2. Its not Logical that one static method access a variable non static, so you should change the logic there!

Greetings :)...

DarkZaioN
  • 51
  • 2