过去几天我一直在研究这个项目,现在我遇到了一个错误。我在google和stackoverflow上搜索了解决方案,但他们没有帮助(或者我不知道如何修复它)。
按下“停止”按钮后,我正试图从TimerActivity
保存计时器中的数据,并将其显示在HistoryActivity
上。
TimerActivity
public class TimerActivity extends AppCompatActivity {
Toolbar toolbar;
private ImageButton imgbtnPause;
private ImageButton imgbtnReset;
private ImageButton imgbtnStart;
private ImageButton imgbtnStop;
private Chronometer mChronometer;
long saveTime;
private long lastPause;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timer);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
imgbtnPause = (ImageButton) findViewById(R.id.imgButtonPause);
imgbtnReset = (ImageButton) findViewById(R.id.imgButtonReset);
imgbtnStart = (ImageButton) findViewById(R.id.imgButtonStart);
imgbtnStop = (ImageButton) findViewById(R.id.imgButtonStop);
mChronometer = (Chronometer) findViewById(R.id.chronometer);
mChronometer.setText("00:00:00");
mChronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer chronometer) {
CharSequence text = chronometer.getText();
if (text.length() == 5) {
chronometer.setText("00:" + text);
} else if (text.length() == 7) {
chronometer.setText("0" + text);
}
}
});
imgbtnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (lastPause != 0) {
mChronometer.setBase(mChronometer.getBase() + SystemClock.elapsedRealtime() - lastPause);
} else {
mChronometer.setBase(SystemClock.elapsedRealtime());
}
mChronometer.start();
imgbtnStart.setEnabled(false);
imgbtnPause.setEnabled(true);
}
});
imgbtnPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
lastPause = SystemClock.elapsedRealtime();
mChronometer.stop();
imgbtnPause.setEnabled(false);
imgbtnStart.setEnabled(true);
}
});
imgbtnReset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mChronometer.stop();
mChronometer.setBase(SystemClock.elapsedRealtime());
lastPause = 0;
imgbtnStart.setEnabled(true);
imgbtnPause.setEnabled(false);
}
});
imgbtnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mChronometer.stop();
final AlertDialog.Builder builder = new AlertDialog.Builder(TimerActivity.this);
builder.setTitle("Stop alert");
builder.setMessage("Have you finished working mate?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mChronometer.stop();
mChronometer.setBase(SystemClock.elapsedRealtime());
imgbtnStart.setEnabled(true);
final AlertDialog.Builder builder1 = new AlertDialog.Builder(TimerActivity.this);
builder1.setTitle("Save");
builder1.setMessage("Do you want to save your progress mate?");
builder1.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
saveTime = SystemClock.elapsedRealtime() - mChronometer.getBase();
String seconds = String.valueOf(saveTime);
Intent i = new Intent(getApplicationContext(), HistoryActivity.class);
i.putExtra("Timer", seconds);
startActivity(i);
mChronometer.stop();
mChronometer.setBase(SystemClock.elapsedRealtime());
imgbtnStart.setEnabled(true);
}
});
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.timer_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.id_language) {
Toast.makeText(this, "Language is clicked", Toast.LENGTH_SHORT).show();
} else if (id == R.id.id_logout) {
Intent intent = new Intent(TimerActivity.this, MainActivity.class);
startActivity(intent);
} else if (id == R.id.id_history) {
Intent intent2 = new Intent(TimerActivity.this, HistoryActivity.class);
startActivity(intent2);
}
return super.onOptionsItemSelected(item);
}
}
TimerActivity xml
<Chronometer
android:id="@+id/chronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="192dp"
android:textColor="#e78200"
android:textSize="32sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
并加上四个图像按钮(开始,停止,暂停和重置)。
HistoryActivity
public class HistoryActivity extends AppCompatActivity{
TextView textView;
Toolbar toolbar_h;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
toolbar_h = (Toolbar) findViewById(R.id.toolbar_history);
setSupportActionBar(toolbar_h);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("Timer");
//The key argument here must match that used in the other activity
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.history_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.id_language) {
Toast.makeText(this, "Language is clicked", Toast.LENGTH_SHORT).show();
} else if (id == R.id.id_logout) {
Intent intent = new Intent(HistoryActivity.this, MainActivity.class);
startActivity(intent);
}else if (id == R.id.id_timer){
Intent intent2 = new Intent(HistoryActivity.this, TimerActivity.class);
startActivity(intent2);
}
return super.onOptionsItemSelected(item);
}
}
HistoyActivity XML
只有一个TextView
。
如何将保存的天文台数据显示在HistoryActivity
?
谢谢