3

I try to reference a TextView in my java file through findViewById(int), but this seems to return null even after calling setContentView(int) with the correct layout file.

Help.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/tvBoxHelp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/help" android:layout_gravity="center" android:textSize="20sp" /> </LinearLayout> 

Help.java

 public class Help extends Activity { private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub setContentView(R.layout.help); tv = (TextView) findViewById(R.id.tvBoxHelp); tv.setText("text"); super.onCreate(savedInstanceState); } } 

Thanks in advance.

1
  • super.onCreate(savedInstanceState) should be called directly after onCreate() Commented Dec 25, 2012 at 7:42

3 Answers 3

4

Your super.onCreate(savedInstanceState); position is causing problem.

Replace your Help.java with this

public class Help extends Activity { private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.help); tv = (TextView) findViewById(R.id.tvBoxHelp); tv.setText("text"); } } 
Sign up to request clarification or add additional context in comments.

3 Comments

Actually that doesn't seem to fix it.
During debug the textview's id seems to be "-1".
I checked your code and it works fine on my computer. Try to clean your code from project tab on eclipse. In your TextView what you want to show? You getting text on TextView or not ?
1

call super method (super.onCreate(savedInstanceState);) before setting your content view

5 Comments

Thanks I should've seen that.
But im changing my contentView after the onCreate()? Getting null with findViewById() any idea?
@MuhammadBabar that shouldn't be the case. I can't tell you exactly where is the issue without checking your code.
@PareshMayani thanks Paresh! In my case i was changing the content view after some time in activity i.e in onCreate() setContentView() was called before and successfully setted, null thing was happening, but now i solved it by doing a trick.
@MuhammadBabar Keep calm and enjoy programming :-)
0
 public class Help extends Activity { private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub // setContentView(R.layout.help); tv = (TextView) findViewById(R.id.tvBoxHelp); tv.setText("text"); //This should be called first super.onCreate(savedInstanceState); super.onCreate(savedInstanceState); } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.