gpt4 book ai didi

java - 无法在 Snackbar 中设置计时器

转载 作者:搜寻专家 更新时间:2023-11-01 09:35:34 28 4
gpt4 key购买 nike

我正在尝试在我的 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;
}

最佳答案

你收到这条消息了吗?

java.lang.NullPointerException: println needs a message

如果是,那是因为您尝试像这样记录一条空消息:

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

您必须初始化显示变量以在第一次运行时具有值。

private String display = "";

关于java - 无法在 Snackbar 中设置计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43563748/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com