SetText在一个新活动中

时间:2017-06-08 13:37:57

标签: java android-intent textview oncreate

我得到了一个带有字符串的意图ia新活动,我想在TextView内显示字符串,我试图在TextView内声明我的onCreate但它崩溃了。我如何才能显示变量"requestbuff"

public class RequestDetail extends MainActivity 
{
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        Intent intent = getIntent();
        String requestbuff =  intent.getExtras().getString("Requête");
        TextView details = (TextView)findViewById(R.id.detailreq);
        details.setText(requestbuff);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detail_req);
    }
}

1 个答案:

答案 0 :(得分:0)

这样可以正常工作。将setContentView放在方法的开头,以便创建视图。之后,您可以使用findViewById()

public class RequestDetail extends MainActivity 
{
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.detail_req);
    Intent intent = getIntent();
    String requestbuff =  intent.getExtras().getString("Requête");
    TextView details = (TextView)findViewById(R.id.detailreq);
    details.setText(requestbuff);
}
}