我正在创建一个简单的应用程序,其中有两个NumberPickers用于选择一定的分钟数和秒数。代码不多,所以我会在这里发布:
public class MainActivity extends Activity implements OnClickListener {
private static final String TAG = "Interval Trainer";
private CountDownTimer countDownTimer;
private boolean timerHasStarted = false;
private Button startButton;
public TextView text;
private final long interval = 1 * 1000;
//Create NumberPickers
NumberPicker numberPicker1 = (NumberPicker) findViewById(R.id.numberPicker1);
NumberPicker numberPicker2 = (NumberPicker) findViewById(R.id.numberPicker2);
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.i(TAG,"Entering onCreate()");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startButton = (Button) this.findViewById(R.id.button);
startButton.setOnClickListener(this);
text = (TextView) this.findViewById(R.id.timer);
//Set min and max values for NumberPickers
numberPicker1.setMaxValue(100);
numberPicker1.setMinValue(0);
numberPicker2.setMaxValue(59); //This is the seconds picker
numberPicker2.setMinValue(0);
Log.i(TAG,"Exiting onCreate()");
}
@Override
public void onClick(View v) {
//Calculate total time from NumberPickers in seconds
long startTime = (numberPicker1.getValue() * 60) + numberPicker2.getValue();
//Create CountDownTimer with values from NumberPickers
countDownTimer = new MyCountDownTimer(startTime, interval);
text.setText(text.getText() + String.valueOf(startTime / 1000)); //should be removed
if(!timerHasStarted) {
countDownTimer.start();
timerHasStarted = true;
startButton.setText("STOP");
} else {
countDownTimer.cancel();
timerHasStarted = false;
startButton.setText("RESTART");
}
//Disable the NumberPickers after 'Start' is pressed
numberPicker1.setEnabled(false);
numberPicker2.setEnabled(false);
}
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
@Override
public void onFinish() {
text.setText("Time's up!");
//re-enable the NumberPickers once countdown is done
numberPicker1.setEnabled(true);
numberPicker2.setEnabled(true);
}
@Override
public void onTick(long millisUntilFinished) {
text.setText("" + millisUntilFinished / 1000);
//Changes the value of the NumberPickers after each tick
}
}
}
我感觉崩溃与我在任何方法之外声明两个NumberPickers相关(这会自动使它们变为静态?)。最初我在onCreate()中有这两行代码,但由于我在内部类和其他方法中需要它们,所以我把它移到了外面。这是导致崩溃的原因吗?如果是这样,我该如何正确地执行此操作并仍然可以访问我的类(包括内部类)的numberPicker1和2?
谢谢!
答案 0 :(得分:4)
你不能像这样发现对象
//Create NumberPickers
NumberPicker numberPicker1 = (NumberPicker) findViewById(R.id.numberPicker1);
NumberPicker numberPicker2 = (NumberPicker) findViewById(R.id.numberPicker2);
在onCreate之后,只有布局加载,所以你没有对象,但尚未初始化。 您可以在
之后在onCreate中初始化对象setContentView(R.layout.activity_main);
例如
// init variable
NumberPicker numberPicker1 = null;
NumberPicker numberPicker2 = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
// After here only your Activity gets the layout objects
numberPicker1 = (NumberPicker) findViewById(R.id.numberPicker1);
numberPicker2 = (NumberPicker) findViewById(R.id.numberPicker2);
}
答案 1 :(得分:2)
您应该将findViewById
置于onCreate
方法内并创建局部变量:
// init variable
NumberPicker numberPicker1, numberPicker2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ...
numberPicker1 = (NumberPicker) findViewById(R.id.numberPicker1);
numberPicker2 = (NumberPicker) findViewById(R.id.numberPicker2);
}
然后,您可以在其他方法中调用这些变量。根据{{3}}:
非静态嵌套类( InnerClasses )可以访问封闭类的其他成员,即使它们被声明为私有。静态嵌套类无权访问封闭类的其他成员。 [...] InnerClass 可以直接访问其封闭实例的方法和字段。