Reputation: 3779
I'm new to android deva and java. I want to display text but my code below does not work.
DisplayText.java
package com.example.DisplayText;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class DisplayText extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv2 = (TextView) this.findViewById(R.id.thetext);
tv2.setText("YO -- ");
setContentView(R.layout.main);
}
}
main.xml
<?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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:id="@+id/thetext"
/>
</LinearLayout>
Many thanks for any help! :)
Upvotes: 0
Views: 3171
Reputation: 34026
put
setContentView(R.layout.main);
before TextView tv2 = (TextView) this.findViewById(R.id.thetext);
to access the TextView from main.xml
Upvotes: 1
Reputation: 5064
Change this code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv2 = (TextView) this.findViewById(R.id.thetext);
tv2.setText("YO -- ");
setContentView(R.layout.main);
}
with this one:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv2 = (TextView) this.findViewById(R.id.thetext);
tv2.setText("YO -- ");
}
Upvotes: 2