无法在Snackbar中设置计时器

时间:2017-04-22 19:34:19

标签: java android timer android-snackbar

我正在尝试在我的Snackbar中设置一个计时器,到目前为止我已经尝试了这个并让计时器工作但不是getTime()方法,我认为可能是这种情况,这就是为什么这不是工作。

如果这个问题太糟糕,我很抱歉,我只将Android作为辅助项目。

public class MainActivity extends AppCompatActivity {

private static final String AUDIO_RECORDER_FILE_EXT = ".3gp";
private static final String AUDIO_RECORDER_FOLDER = "VRemind";
private MediaRecorder recorder = null;
private int currentFormat = 0;
private int output_format = MediaRecorder.OutputFormat.THREE_GPP;
private String file_ext = AUDIO_RECORDER_FILE_EXT;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

   final CountDownTimer t;


    t = new CountDownTimer( Long.MAX_VALUE , 1000) {

        int cnt=0;

        @Override
        public void onTick(long millisUntilFinished) {

            cnt++;
            long millis = cnt;
            int seconds = (int) (millis / 60);

            setTime(cnt);
            Log.d("Count:", ""+cnt);
        }

        @Override
        public void onFinish() {

        }
    };

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    final Snackbar snackbar = Snackbar.make(findViewById(R.id.root_layout), getTime(), Snackbar.LENGTH_INDEFINITE);

    final FloatingActionButton fabAdd = (FloatingActionButton) findViewById(R.id.fabAdd);
    final FloatingActionButton fabStop = (FloatingActionButton) findViewById(R.id.fabStop);

    fabAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            t.start();
            fabAdd.setVisibility(View.GONE);
            fabStop.setVisibility(View.VISIBLE);
            snackbar.show();
            snackbar.setAction("CANCEL", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    snackbar.dismiss();
                    fabAdd.setVisibility(View.VISIBLE);
                    fabStop.setVisibility(View.GONE);
                }
            });

        }
    });

    fabStop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            t.cancel();
            snackbar.dismiss();
            fabStop.setVisibility(View.GONE);
            fabAdd.setVisibility(View.VISIBLE);
        }
    });




}




private String display;

public void setTime(int rawCount) {

    int rc = rawCount;

    int minutes = (rc - (rc % 60)) / 60;
    int seconds = (rc % 60);

    String mins = String.format(Locale.ENGLISH, "%02d", minutes);
    String secs = String.format(Locale.ENGLISH, "%02d", seconds);
    display = mins+ ":" +secs;

    Log.d("CountTwo:",display);
    getTime();

}

public String getTime() {

    Log.d("Count getTime:", display);
    return display;
}

2 个答案:

答案 0 :(得分:2)

你收到这条消息吗?

  

java.lang.NullPointerException:println需要一条消息

如果是,那是因为您尝试记录如下的空消息:

Log.d("Count getTime:", display);

您必须初始化显示变量以获得第一次运行的值。

private String display = "";

答案 1 :(得分:0)

我调查了它,问题是Snackbar无法访问字符串display,而且Snackbar测试无法动态更新所以我做了这两个并在这里和那里进行了一些更改我的代码:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

       final CountDownTimer t;


        t = new CountDownTimer( Long.MAX_VALUE , 1000) {

            int cnt=0;

            @Override
            public void onTick(long millisUntilFinished) {

                cnt++;
                long millis = cnt;
                int seconds = (int) (millis / 60);

                setTime(cnt);
                Log.d("Count:", ""+cnt);
            }

            @Override
            public void onFinish() {
                cnt = 0;
            }

        };

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        snackbar = Snackbar.make(findViewById(R.id.root_layout), "", Snackbar.LENGTH_INDEFINITE);

        final FloatingActionButton fabAdd = (FloatingActionButton) findViewById(R.id.fabAdd);
        final FloatingActionButton fabStop = (FloatingActionButton) findViewById(R.id.fabStop);

        fabAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                t.start();
                fabAdd.setVisibility(View.GONE);
                fabStop.setVisibility(View.VISIBLE);
                snackbar.show();
                snackbar.setAction("CANCEL", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        t.cancel();
                        t.onFinish();
//                        setTime(0);
                        snackbar.dismiss();
                        fabAdd.setVisibility(View.VISIBLE);
                        fabStop.setVisibility(View.GONE);
                    }
                });

            }
        });

        fabStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                t.cancel();
                t.onFinish();
//                setTime(0);
                snackbar.dismiss();
                fabStop.setVisibility(View.GONE);
                fabAdd.setVisibility(View.VISIBLE);
            }
        });




    }


    /*
        public void Duration() {
        *//*        Timer timer = new Timer();

            TimerTask timerTask = new TimerTask() {
                @Override
                public void run() {

                    runOnUiThread(new Runnable() {
                        int count;

                        @Override
                        public void run() {
                            setTime(count);
                            count++;
                            Log.d("Count:", ""+count);
                        }
                    });
                }
            };*//* //Old code removed on 22Apr17@11:41PM


        }*/ //Old code Duration method

     String display="";

    public void setTime(int rawCount) {

//        int rc = rawCount;

        int minutes = (rawCount - (rawCount % 60)) / 60;
        int seconds = (rawCount % 60);

        String mins = String.format(Locale.ENGLISH, "%02d", minutes);
        String secs = String.format(Locale.ENGLISH, "%02d", seconds);
        display = mins+ ":" +secs;

        Log.d("CountTwo:",display);
        snackbar.setText(display);

    }

    /*public String getTime() {

        Log.d("Count getTime:", display);
        return display;
    }*/