1
我有兩個類。 A級是活動我的進度條將被使用。和B類是無活動我的進度條將被更新。但是當我從非活動類B調用進度條時。我得到了空指針異常。進度條給NullPointerException
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.**
請任何一個幫助我。
你必須給B類進度條的參考。然後使用它,並解僱它 – abhi
但我怎麼可能做plz? – user2160008
發送進度條作爲參數在classB構造函數中從ActivityA中使用它在classb中 – abhi