PDA

View Full Version : Could not find a TextView by calling findViewById method



zhangzhexin_81
07-19-2011, 09:06 AM
Hello, I cannot find a TextView by calling findViewById method even through the ID does exists.
please see my code:
Activity:
public class OtherActivity extends Activity {

protected void onCreate(Bundle savedInstanceState) {
TextView textView = (TextView)findViewById(R.ID.txt02);
super.onCreate(savedInstanceState); // line 5
setContentView(R.layout.other_activity);
...
}
...
}
I add a breakpoint at line5 and start project in debug mode, when the executing stopped at the break point, I moved the mouse to variable textView, it is null.
You can see my other_activity.xml under layout folder like below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView
android:id="@+ID/txt02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

and the content in R.java like below:
public final class R {
public static final class ID {
public static final int Activity01_btn=0x7f050001;
public static final int otherActivity_Btn=0x7f050003;
public static final int txt01=0x7f050000;
public static final int txt02=0x7f050002;
}
public static final class attr {
}
public static final class drawable {
public static final int icon=0x7f020000;
}
public static final class layout {
public static final int main=0x7f030000;
public static final int other_activity=0x7f030001;
}582
public static final class string {
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
public static final int other=0x7f040002;
public static final int otherActivityBtn=0x7f040003;
}
}

So, I don't think the TextView could not be found, but why the variable textView is null. I was confused.
could you please help me ?
Additional, the attached is my source files.

alostpacket
07-20-2011, 11:11 PM
you have to call setContentView(layoutID); first

zhangzhexin_81
07-21-2011, 06:48 PM
Thanks very much!:android-party: