1

My application stops when I declare the TextView outside the onCreate method, I'm doing this because I need do access the TextView variable from other methods as well. I'll be grateful for any kind of help.

public class MainActivity extends ActionBarActivity {
    TextView textView = (TextView)findViewById(R.id.defaultText);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);

        textView.setText("Hello");
    }
} 
Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
Albert Lekaj
  • 11
  • 1
  • 2
  • Just define your TextView as a class scoped variable (ie, place it at the top of your class outside of any method scope. – Booger Sep 02 '14 at 22:02
  • Look in Logcat for the actual error message. When you ask a question like this, include the Logcat info with your question. – Dale Wilson Sep 02 '14 at 22:05
  • @Dale the answer is clear when reading the code. – TehCoder Sep 02 '14 at 22:06
  • 1
    @TehCoder True, but if the OP had looked at Logcat he wouldn't have needed to ask the question, and this question is not likely to help future SO users. – Dale Wilson Sep 02 '14 at 22:07

5 Answers5

4
My application stops when I declare the TextView outside the onCreate method

That is because the Layout is not yet inflated in your activity thus crashing you app and I am 100% sure the error is NPE when you set the text here : textView.setText("Hello");.

Solution:

always initialized your TextView inside your Oncreate after setContentView and having your textView object as a global instance.

public class MainActivity extends ActionBarActivity {
   TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);

        textView = (TextView)findViewById(R.id.defaultText);
        textView.setText("Hello");
    }
}
Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
3

You can't declare a View outside a function.

TextView tv; and in OnCreate do: tv = (TextView)findViewById(...)

TehCoder
  • 209
  • 1
  • 8
0

As others pointed out, the error you are getting is because the view containing the TextView as a child as not yet been inflated. You should declare the strong reference and then instantiate the variable on the onCreate() method. If you insist on using such a code then I recommend you to take a look at Dependency injection.

mradzinski
  • 614
  • 1
  • 8
  • 21
0

To do this:

TextView textView = (TextView)findViewById(R.id.defaultText);

You first set the content view to the Activity if not will not work.

Android
  • 1,420
  • 4
  • 13
  • 23
-1

I faced this problem and i solved it by using (tag) for my view (myTextView.tag) you can lean more about this property from here:

What is the main purpose of setTag() getTag() methods of View?

I mean I declare a variable called (counter) outside onCreate method, then i find my view wherever i need using his tag.

hope this helping