简体   繁体   中英

Set text to a textView

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??

您需要先调用setContentView(your_layout)然后才能调用

TextView text = (TextView) findViewById(R.id.textView1);

您需要将setContentView与包含textview1layout一起使用,即您需要在Activity定义layout

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");
        }

Hi, you have to use

setContentView(R.layout.your_layout);

try this

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");

    }
}

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");





}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM