non static setGravity cannot be referenced from static context

David Agabi

I'm very new to android development, but know a little Java. I am having problems knowing where to place Toast.setGravity(Gravity.TOP,0,0) in my code.

Here's the code:

package ca.ignitestudio.geoquiz;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


public class QuizActivity extends AppCompatActivity {

private Button mTrueButton;
private Button mFalseButton;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_quiz);



    mTrueButton = (Button) findViewById(R.id.true_button);
    mTrueButton.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            Toast.makeText(QuizActivity.this, R.string.correct_toast, Toast.LENGTH_SHORT).show();
            Toast.setGravity(Gravity.TOP,0,0);
        }
    });
    mFalseButton = (Button) findViewById(R.id.false_button);
    mFalseButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(QuizActivity.this, R.string.incorrect_toast, Toast.LENGTH_SHORT).show();
        }
    });
}

}

android_Muncher

You will have to call setGravity this way:

 Toast toast = Toast.makeText(test.this,"bbb", Toast.LENGTH_LONG);
    toast.setGravity(Gravity.CENTER, 0, 0);
    toast.show();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Why does String::isEmpty works when non-static method cannot be referenced from a static context?

From Java

java - Non-static method 'getLogger' cannot be referenced from a static context

From Java

non-static class cannot be referenced from a static context

From Java

Java Generics: non-static type variable T cannot be referenced from a static context

From Java

non static method cannot be referenced from a static context

From Java

"non-static variable this cannot be referenced from a static context"?

From Java

Cannot be referenced from static context error

From Java

Java - cannot be referenced from a static context

From Dev

java: non-static variable this cannot be referenced from a static context

From Dev

Notification `notify()` cannot be referenced from a static context

From Dev

Map.merge .. non-static method cannot be referenced from static context

From Dev

Non static variable cannot be referenced from static context java

From Dev

non-static method count(int) cannot be referenced from a static context

From Dev

non-static variable s cannot be referenced from a static context

From Dev

"Non-static method cannot be referenced from static context" error

From Dev

Android - executePendingTransactions cannot be referenced from static context

From Dev

non static variable this cannot be referenced from a static context

From Dev

non-static variable cannot be referenced from a static context java

From Dev

Error:non static method 'edit' cannot be referenced in static context

From Dev

non-static method getIntent() cannot be referenced from a static context

From Dev

Non-static variable filepath cannot be referenced from a static context

From Dev

Non-static edit() cannot be referenced from a static context

From Dev

Non-static variable cannot be referenced from a static context?

From Dev

non-static variable cannot be referenced from static context [JAVA]

From Dev

non-static method matcher(CharSequence) cannot be referenced from a static context

From Dev

Error: Non-static variable super cannot be referenced from a static context >>but i use static keyword

From Dev

Cannot be referenced from a static context

From Dev

Non-static method getSocketFactory cannot be referenced from a static context

From Dev

Can't call constructor of inner class in a static context -- "non-static variable this cannot be referenced from a static context"

Related Related

  1. 1

    Why does String::isEmpty works when non-static method cannot be referenced from a static context?

  2. 2

    java - Non-static method 'getLogger' cannot be referenced from a static context

  3. 3

    non-static class cannot be referenced from a static context

  4. 4

    Java Generics: non-static type variable T cannot be referenced from a static context

  5. 5

    non static method cannot be referenced from a static context

  6. 6

    "non-static variable this cannot be referenced from a static context"?

  7. 7

    Cannot be referenced from static context error

  8. 8

    Java - cannot be referenced from a static context

  9. 9

    java: non-static variable this cannot be referenced from a static context

  10. 10

    Notification `notify()` cannot be referenced from a static context

  11. 11

    Map.merge .. non-static method cannot be referenced from static context

  12. 12

    Non static variable cannot be referenced from static context java

  13. 13

    non-static method count(int) cannot be referenced from a static context

  14. 14

    non-static variable s cannot be referenced from a static context

  15. 15

    "Non-static method cannot be referenced from static context" error

  16. 16

    Android - executePendingTransactions cannot be referenced from static context

  17. 17

    non static variable this cannot be referenced from a static context

  18. 18

    non-static variable cannot be referenced from a static context java

  19. 19

    Error:non static method 'edit' cannot be referenced in static context

  20. 20

    non-static method getIntent() cannot be referenced from a static context

  21. 21

    Non-static variable filepath cannot be referenced from a static context

  22. 22

    Non-static edit() cannot be referenced from a static context

  23. 23

    Non-static variable cannot be referenced from a static context?

  24. 24

    non-static variable cannot be referenced from static context [JAVA]

  25. 25

    non-static method matcher(CharSequence) cannot be referenced from a static context

  26. 26

    Error: Non-static variable super cannot be referenced from a static context >>but i use static keyword

  27. 27

    Cannot be referenced from a static context

  28. 28

    Non-static method getSocketFactory cannot be referenced from a static context

  29. 29

    Can't call constructor of inner class in a static context -- "non-static variable this cannot be referenced from a static context"

HotTag

Archive