我有两节课。A 类是使用我的进度条的活动。B 类没有活动,我的进度条将更新。但是当我从非活动类 B 调用进度条时。我得到空指针异常。
A类:-
ProgressBar progressBar;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);}
B类:-
new Thread(new Runnable() {
int i = 0;
int progressStatus = 0;
public void run() {
while (progressStatus < 100) {
progressStatus += doWork();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Update the progress bar
((Activity) cnt).runOnUiThread(new Runnable() {
public void run() {
XMPPClient xc = new XMPPClient();
xc.progressBar = new ProgressBar(cnt);
xc.progressBar.setProgress(progressStatus);
// Toast.makeText(cnt, "ok", Toast.LENGTH_SHORT).show();
i++;
}
});
}
}
private int doWork() {
return i * 3;
}
}).start();
when i added this line :- xc.progressBar = new ProgressBar(cnt);
then i did not get nullpointerexception. But now my progress bar is **not updating.**
请任何人帮助我。