我动态创建了一个文本视图,但问题是当我想在单击按钮时设置此文本视图的文本大小时,我得到:
错误:null对象引用。
代码:
//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.
编辑:这个问题在另一个问题中被视为另一个问题的副本 答案是为什么(空对象引用)错误发生,我知道问题的原因我需要的是如何为活动中的非现有对象设置值的答案。
答案 0 :(得分:0)
点击UITableViewController
mbutton1