对于我的学校项目,我目前正在进行简单的锻炼应用程序。但是我陷入了倒计时计时器上,因为应用程序不断崩溃。首先,有一些受欢迎的屏幕;然后,您继续进行自己的锻炼(选择锻炼,休息和回合数)。它运行良好,甚至可以使用意图将所有这些变量进入下一阶级。这是按钮下一个代码:
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(CustomWorkout.this, GetReady.class);
myIntent.putExtra("all_exercises", Exercises); //arraylist of exercises
myIntent.putExtra("total_rounds", total_rounds); //number of rounds
myIntent.putExtra("total_rest", total_rest); //rest time
startActivity(myIntent);
}
});
如您所见,我然后调用 GetReady()类,该类是简单的5秒倒计时计时器,以准备自己进行首次锻炼。 GetReady 看起来像这样:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.getready);
getReady();
}
public void getReady(){
timer = (TextView) findViewById(R.id.timer);
cTimer= new CountDownTimer(5000, 1000) {
public void onTick(long millisUntilFinished) {
timer.setText("" + millisUntilFinished / 1000);
}
public void onFinish() {
startActivity(new Intent(GetReady.this, Exercises.class));
}
}.start();
}
现在遇到了错误(以及应用程序崩溃)。在倒计时完成时,我调用 corest() class(这是带有当前练习和按钮的名称的屏幕 ->单击时,您再次开始倒计时计时器)。代码:
Intent i = getIntent();
all_exercises = (ArrayList<String>) i.getSerializableExtra("all_exercises"); //getting exercises from Intent set in first class
rounds=(int)i.getSerializableExtra("total_rounds");
exercise_name = (TextView) findViewById(R.id.exercise_name);
exercise_name.setText(all_exercises.get(0));
final Button done=(Button) findViewById(R.id.done);
done.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent in = new Intent(Exercises.this, Countdown.class); //starting Countdown
startActivity(in);
}
});
正常倒计时,然后发生崩溃。如果我在头等舱中设置该按钮应该调用练习类,而不是准备就绪,它不会崩溃(但它在倒计时类中崩溃)。
崩溃日志:
FATAL EXCEPTION: main
Process: com.msimonic.worldofworkout, PID: 8968
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.msimonic.worldofworkout/com.msimonic.worldofworkout.Exercises}: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
at com.msimonic.worldofworkout.Exercises.onCreate(Exercises.java:39)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
您必须在 getready 中传递数据,以便可以在练习中访问它们。为了少写代码,我将它们放入Bundle
中,然后简单地转移此Bundle
:
在OnClickListener
中:
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(CustomWorkout.this, GetReady.class);
Bundle b = new Bundle();
b.putSerializable("all_exercises", Exercises); //arraylist of exercises
b.putInt("total_rounds", total_rounds); //number of rounds
b.putInt("total_rest", total_rest); //rest time
myIntent.putExtra("myData", b);
startActivity(myIntent);
}
});
in getready.java :
public void getReady(){
timer = (TextView) findViewById(R.id.timer);
cTimer= new CountDownTimer(5000, 1000) {
public void onTick(long millisUntilFinished) {
timer.setText("" + millisUntilFinished / 1000);
}
public void onFinish() {
Bundle b = GetReady.this.getIntent().getBundleExtra("myData");
Intent myNextIntent = new Intent(GetReady.this, Exercises.class);
myNextIntent.putExtra("myDataFromGetReady", b);
startActivity(myNextIntent);
}
}.start();
}
in excercises.java :
Intent i = getIntent();
Bundle b = i.getBundleExtra("myDataFromGetReady");
// now get the data from the Bundle:
all_exercises = (ArrayList<String>) b.getSerializable("all_exercises");
rounds= b.getInt("total_rounds", -1);
exercise_name = (TextView) findViewById(R.id.exercise_name);
exercise_name.setText(all_exercises.get(0));
final Button done=(Button) findViewById(R.id.done);
// ... and so on ...