フラグメント(ActionBarSherlockタブ)があり、バックグラウンドでクロノメーターを実行したい:
public class BFragment extends SherlockFragment {
private Button start;
private View v;
private Chronometer chrono;
private Button pause;
private Button reset;
private long lastPause;
private Button resume;
private boolean isChronometerRunning = false;
private boolean richtig = false;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
v = inflater.inflate(R.layout.activity_bfragment, container, false);
start = (Button) v.findViewById(R.id.start);
chrono = (Chronometer) v.findViewById(R.id.chronometer1);
pause = (Button) v.findViewById(R.id.pause);
reset = (Button) v.findViewById(R.id.reset);
resume = (Button) v.findViewById(R.id.resume);
new ChronoBackground().execute();
return v;
}
void StartTimer() {
start.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
chrono.setBase(SystemClock.elapsedRealtime());
chrono.start();
isChronometerRunning = true;
System.out.println(SystemClock.elapsedRealtime());
}
});
pause.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
lastPause = SystemClock.elapsedRealtime();
chrono.stop();
isChronometerRunning = false;
richtig = true;
}
});
resume.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (isChronometerRunning == false && richtig) {
chrono.setBase(chrono.getBase() + SystemClock.elapsedRealtime() - lastPause);
chrono.start();
richtig = false;
} else {
}
}
});
reset.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
chrono.setBase(SystemClock.elapsedRealtime());
System.out.println(chrono.getBase());
}
});
}
}
そして、これが私のChronBackgroundクラス/コードです:
public class ChronoBackground extends AsyncTask<Void, Void, Void> {
/** The system calls this to perform work in a worker thread and
* delivers it the parameters given to AsyncTask.execute() */
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
StartTimer();
return null;
}
/** The system calls this to perform work in the UI thread and delivers
* the result from doInBackground() */
@Override
protected void onPostExecute(Void result) {
return ;
}
@Override
protected void onPreExecute() {
return ;
}
}
エラーが発生しました。クロノがこのように機能しているかどうかはわかりません...エラーはChronoBackgroundにあります:StartTimer()が定義されていません。