Java 调用构造的textview时的空对象引用

Java 调用构造的textview时的空对象引用,java,android,Java,Android,我动态创建了一个文本视图,但问题是,当我想在单击按钮时设置此文本视图的文本大小时,我得到: 错误:空对象引用 守则: //in Main class TextView textview; //creating the text view on button click Button mbutton1=(Button)findViewById(R.id.button1); mbutton1.setOnClickListener(new View.OnClickListener(){ @Ove

我动态创建了一个文本视图,但问题是,当我想在单击按钮时设置此文本视图的文本大小时,我得到:

错误:空对象引用

守则:

//in Main class

TextView textview;


//creating the text view on button click
Button mbutton1=(Button)findViewById(R.id.button1);
mbutton1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
 textview=new TextView(this);

//adding the view to the relativelayout
RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams
(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT );
textview.setLayoutParams(params);

//making object for Relativelayout and add the view to activity
RelativeLayout relativel=(RelativeLayout)findViewById(R.id.relativelayout);
relative1.addView(textview);
}
});

//now here is the button which must set the size of this text view
 Button mbutton2=(Button)findViewById(R.id.button2);
mbutton2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
textview.setsize(20);

}
});
//And the error is when I click button 2 it says null object reference.
编辑:此问题作为另一个问题的副本关闭,但在另一个问题中
答案是(空对象引用)错误发生的原因,我知道问题的原因我需要的是一个关于如何为活动中不存在的对象设置值的答案。

在单击
mbutton2
之前,您必须单击
mbutton1
在单击
mbutton2
之前,您是否单击了
mbutton1
?textview=新建textview(此);把这个放在按钮外面click@Redman我希望在单击按钮时创建文本视图。您从何处调用此函数?@Ezio from Oncreate方法。我希望在将文本视图发布到活动之前设置文本视图的文本。