我试图通过此活动成功传递一个int:
public class SecondActivity extends Activity {
private int sign=3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TestView newView = new TestView(this, sign);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(newView);
}
}
这个观点:
public class TestView extends View {
private int sign;
public TestView(Context context, int signValue) {
super(context);
this.sign=signValue;
}
}
但是值没有传递,变量“sign”保持为null。我错过了什么吗?什么?
(在我的代码中,变量符号从包中获取其值,但我测试了它并且知道它获得了正确的值。)
修改: 仍然不知道前面的代码有什么问题,但是我通过创建一个setSign方法来解决它。
public class SecondActivity extends Activity {
private int sign=3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TestView newView = new TestView(this, sign);
requestWindowFeature(Window.FEATURE_NO_TITLE);
newView.setSign(sign);
setContentView(newView);
}
}
这个观点:
public class TestView extends View {
private int sign=0;
public TestView(Context context) {
super(context);
}
public void setSign(int signValue) {
sign=signValue;
}
}