如何为最终变量赋值
How to assign value to a final variable
变量"startTs"需要声明为final。当我宣布它为最终版本时 android studio 突出显示它并说 "cant assign value to final variable"
如果删除最终修饰符,我会收到 "variable must be final"
现在我想使用变量 "startTs" 并为其赋值,该怎么做
请告诉我如何解决这个问题
代码:
final long startTs;
mbtnLogFileAction.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (action) {
case "Record":
Log.i(TAG, CSubTag.bullet("ATRx.onPreExecute", "start Recording logs"));
mbtnLogFileAction.setText("Pause");
metLogFileName.setEnabled(false);
String logFileName = metLogFileName.getText().toString();
if (logFileName != null && !logFileName.isEmpty()) {
if (!mLogFileHT.containsKey(logFileName)) {
startTs = TimeUtils.getTSSec();//cant assign value to final variable
mLogFileHT.put(logFileName, startTs);
} else {
startTs = mLogFileHT.get(logFileName);//cant assign value to final variable
}
因为您使用的是匿名内部 class ,startTs
必须在其中使用 final 并且因为它是 final ,所以它应该是一个编译时间常量并且不能被重新分配
只需将 startTs 全局放置即可。
public class YourClass
{
long startTs;
// your other code goes here..
mbtnLogFileAction.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (action) {
case "Record":
Log.i(TAG, CSubTag.bullet("ATRx.onPreExecute", "start Recording logs"));
mbtnLogFileAction.setText("Pause");
metLogFileName.setEnabled(false);
String logFileName = metLogFileName.getText().toString();
if (logFileName != null && !logFileName.isEmpty()) {
if (!mLogFileHT.containsKey(logFileName)) {
startTs = TimeUtils.getTSSec();//cant assign value to final variable
mLogFileHT.put(logFileName, startTs);
} else {
startTs = mLogFileHT.get(logFileName);//cant assign value to final variable
}
}
final成员变量必须在声明时或构造函数内部初始化,否则会导致编译错误。
变量"startTs"需要声明为final。当我宣布它为最终版本时 android studio 突出显示它并说 "cant assign value to final variable" 如果删除最终修饰符,我会收到 "variable must be final"
现在我想使用变量 "startTs" 并为其赋值,该怎么做
请告诉我如何解决这个问题
代码:
final long startTs;
mbtnLogFileAction.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (action) {
case "Record":
Log.i(TAG, CSubTag.bullet("ATRx.onPreExecute", "start Recording logs"));
mbtnLogFileAction.setText("Pause");
metLogFileName.setEnabled(false);
String logFileName = metLogFileName.getText().toString();
if (logFileName != null && !logFileName.isEmpty()) {
if (!mLogFileHT.containsKey(logFileName)) {
startTs = TimeUtils.getTSSec();//cant assign value to final variable
mLogFileHT.put(logFileName, startTs);
} else {
startTs = mLogFileHT.get(logFileName);//cant assign value to final variable
}
因为您使用的是匿名内部 class ,startTs
必须在其中使用 final 并且因为它是 final ,所以它应该是一个编译时间常量并且不能被重新分配
只需将 startTs 全局放置即可。
public class YourClass
{
long startTs;
// your other code goes here..
mbtnLogFileAction.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (action) {
case "Record":
Log.i(TAG, CSubTag.bullet("ATRx.onPreExecute", "start Recording logs"));
mbtnLogFileAction.setText("Pause");
metLogFileName.setEnabled(false);
String logFileName = metLogFileName.getText().toString();
if (logFileName != null && !logFileName.isEmpty()) {
if (!mLogFileHT.containsKey(logFileName)) {
startTs = TimeUtils.getTSSec();//cant assign value to final variable
mLogFileHT.put(logFileName, startTs);
} else {
startTs = mLogFileHT.get(logFileName);//cant assign value to final variable
}
}
final成员变量必须在声明时或构造函数内部初始化,否则会导致编译错误。