I'm a beginner with both Java and Android programing. I'm trying to create a TextView
and then edit its content within Java code. This is the TextView
definition:
<TextView android:id="@+id/NewTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/NewTVS"
android:textSize="20sp" />
And this is the Java code (the needed part, everything else is default):
@Override
protected void onCreate(Bundle savedInstanceState) {
TextView mNewTV = (TextView)findViewById(R.id.NewTV);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String newstr = "hello";
mNewTV.setText(newstr);
}
Launching this code causes a problem - the app closing itself. I believe that the problem occurs during the findViewById
command.
Hopefully I was clear enough despite my bad English. Thanks in advance.
you are trying to get reference of TextView mNewTV
before setting setContentView(R.layout.activity_main);
the order must be like this :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView mNewTV = (TextView)findViewById(R.id.NewTV);
String newstr = "hello";
mNewTV.setText(newstr);
}