Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android错误:未定义的方法_Android_Android Asynctask - Fatal编程技术网

Android错误:未定义的方法

Android错误:未定义的方法,android,android-asynctask,Android,Android Asynctask,我有一个片段(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;

我有一个片段(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());

        }
    });
}
}
这是我的背景类/代码:

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 ;
}
}
public类ChronoBackground扩展异步任务{
/**系统调用它以在工作线程中执行工作,并
*将给定给AsyncTask.execute()的参数传递给它*/
@凌驾
受保护的Void doInBackground(Void…arg0){
//TODO自动生成的方法存根
StartTimer();
返回null;
}
/**系统调用它来执行UI线程中的工作,并提供
*doInBackground()的结果*/
@凌驾
受保护的void onPostExecute(void结果){
返回;
}
@凌驾
受保护的void onPreExecute(){
返回;
}
}

现在有一个错误,我不确定我的计时器是否是这样工作的。。。错误在ChronoBackground:StartTimer()未定义。

错误可能在于,ChronoBackground不是BFragment的内部类,并且没有看到该方法?

好的,我将公共类ChronoBackground更改为私有类ChronoBackground,并将其移动到我的BFragment中。。。但当我启动应用程序并更改视图或选项卡时,计时器将重置为00:00,尽管它应该在后台运行-我做错了什么?好吧,这是另一个问题!您需要将计时器设置为
服务
,使其在后台运行。AsyncTask用于两个线程—GUI和“逻辑”,但您需要一个“在逻辑后面”的线程,而这正是服务所做的。在这里搜索,它就在附近。如果您也使用了,请确保将答案标记为正确;)