请在这件事上给予我帮助。我创建了一个进度条,并在应用程序启动后每隔 100 毫秒以编程方式更新其进度(是的,这听起来很奇怪,但只是为了搞乱目的)。但每次运行它时,我都会收到 NullPointerException。有人可以帮我解决这个问题吗?日志表明 NullPointerException 发生在“custom_handler.sendMessage(message);”处 在下面。太感谢了。
private Handler custom_handler, main_handler;
private int progress = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//TextView to display the
text_view = (TextView)findViewById(R.id.text_view); progress
progress_bar = (ProgressBar)findViewById(R.id.progress_bar);
//Instantiate a new worker thread
//but somehow its handler is not instantiated
//by the time the compiler reaches the "custom_handler.sendMessage(message);"
//at all, keep getting NullPointerException
//please look at the comment below the next code block.
new MyThread().start();
main_handler = new Handler(Looper.getMainLooper()) {
public void handleMessage(Message …Run Code Online (Sandbox Code Playgroud)