Reputation: 13
I want to set text to my textView1 but i cant because my app crashes! What can i do? Please help!!! :(
public class settext extends Activity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView text = (TextView) findViewById(R.id.textView1);
text.setText("how to set Text");
}
}
When i start the application nothing appears and it crashes all the time i start it.... Does anyone know where i can find a way to read about programming in a site??
Upvotes: 1
Views: 115
Reputation: 690
You need to use setContentView
with the layout
which contains textview1
i.e You need to define the layout
in your Activity
Upvotes: 1
Reputation: 780
Your app is getting crash because your class does not have a reference of your activity layout. You must set layout.
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//Set setContentView in your code for example //setContentView(R.layout.activity_main);
//activity_main is my layout in projectfolder> res> layout> activity_main.xml
// so put reference of your layout in it.
setContentView(R.layout.<yourLayout>);
TextView text = (TextView) findViewById(R.id.textView1);
text.setText("how to set Text");
}
Upvotes: 1
Reputation: 3417
Your app will really crash because you didn't put the setContentView() code in your code:
try this one:
public class settext extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout); //PUT THIS ON YOUR CODE
TextView text = (TextView) findViewById(R.id.textView1);
text.setText("how to set Text");
}
}
Upvotes: 1
Reputation: 1451
Actually you need to add a layout (View) in which textView1 would be there For example try this :
setContentView(R.layout.x1);
so your code would be like that
public class settext extends Activity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.x1);
TextView text = (TextView) findViewById(R.id.textView1);
text.setText("how to set Text");
}
Upvotes: 0
Reputation: 13520
You need to call setContentView(your_layout)
before you can call
TextView text = (TextView) findViewById(R.id.textView1);
Upvotes: 1