gpt4 book ai didi

java - 如何初始化 TextView 以避免 NullPointer 错误?

转载 作者:行者123 更新时间:2023-11-29 15:36:16 27 4
gpt4 key购买 nike

我正在尝试学习如何使用绑定(bind)服务。每当我单击“开始/暂停”按钮时,我都希望计时器根据需要进行更改。但是,单击这些按钮将终止该程序。我不确定为什么这个 TextView 对象在我的服务类中给出 NullPointerException 错误。有人可以帮我解决这个问题吗?

public class LocalService extends Service {
//Binder
private final IBinder mBinder = new LocalBinder();

Button btnStart,btnPause,btnLap;
TextView txtTimer;
Handler customHandler = new Handler();
LinearLayout container;

long startTime=0L,timeInMilliseconds=0L,timeSwapBuff=0L,updateTime=0L;


public class LocalBinder extends Binder {
LocalService getService() {
//Return this instance of LocalService so public methods can be called
return LocalService.this;
}
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}

Runnable updateTimerThread = new Runnable() {
@Override
public void run() {
timeInMilliseconds = SystemClock.uptimeMillis()-startTime;
updateTime = timeSwapBuff+timeInMilliseconds;
int secs=(int)(updateTime/1000);
int mins=secs/60;
secs%=60;
int milliseconds=(int)(updateTime%1000);
txtTimer.setText(""+mins+":"+String.format("%02d",secs)+":"
+String.format("%03d",milliseconds));
customHandler.postDelayed(this,0);
}
};
}

此对象已在我的主类中初始化:

public class MainActivity extends AppCompatActivity {

LocalService mService = new LocalService();
boolean mBound = false;
Button btnStart, btnPause, btnLap;
TextView txtTimer;
LinearLayout container;
Handler customHandler = new Handler();
long startTime=0L,timeInMilliseconds=0L,timeSwapBuff=0L,updateTime=0L;


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

btnStart = (Button)findViewById(R.id.btnStart);
btnPause = (Button)findViewById(R.id.btnPause);
btnLap = (Button)findViewById(R.id.btnLap);
txtTimer = (TextView)findViewById(R.id.timerValue);
container = (LinearLayout)findViewById(R.id.container);

btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startTime = SystemClock.uptimeMillis();
customHandler.postDelayed(mService.updateTimerThread,0);

}
});

btnPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
timeSwapBuff=+timeInMilliseconds;
customHandler.removeCallbacks(mService.updateTimerThread);
}
});

btnLap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View addView = inflater.inflate(R.layout.row,(ViewGroup)null);
TextView txtValue = (TextView)addView.findViewById(R.id.textContent);
txtValue.setText(txtTimer.getText());
container.addView(addView);
}
});
}

@Override
protected void onStart() {
super.onStart();
//Bind to LocalService
Intent intent = new Intent(this, LocalService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}

@Override
protected void onStop() {
super.onStop();
unbindService(mConnection);
mBound = false;
}

//Define callbacks for service binding, passed to bindService()
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName mainActivity, IBinder service) {
//We've bound to LocalService, cast the IBinder and get LocalService instance
LocalService.LocalBinder binder = (LocalService.LocalBinder) service;
mService = binder.getService();
mBound = true;
}

@Override
public void onServiceDisconnected(ComponentName componentName) {

}
};
}

当此服务运行时,我希望我的主要 Activity 中的 txtTimer 能够使用绑定(bind)的服务值进行更新。我收到一个 NullPointer 错误代码,将我带到第 56 行附近的 LocalService 类:

txtTimer.setText(""+mins+":"+String.format("%02d",secs)+":"
+String.format("%03d",milliseconds));

什么是最好的更新方式,以便 txtTimer 能够显示变化的值?我真的很感激一些帮助

最佳答案

LocalService 类上定义此构造函数:

public LocalService(TextView textView){
txtTimer = textView;
super();
}

并在 btnStart.setOnClickListener(new View ... 之前添加此行:

mService = new LocalService(txtTimer);

希望对你有帮助

关于java - 如何初始化 TextView 以避免 NullPointer 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49365526/

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