1

我有两节课。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.**

请任何人帮助我。

4

2 回答 2

4
ProgressBar progressBar; 

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    progressBar = (ProgressBar) findViewById(R.id.progressBar1);

    ClassB classb = new ClassB(this, progressBar);
}

public class ClassB{

    private Context cnt;
    private ProgressBar progressBar;

    public ClassB(Context context, ProgressBar pBar){
         cnt = context;
         progressBar = pBar;
    }
}

现在您可以使用progressBar 而不是像之前的代码那样创建一个新的!只是一个快速的概述......

于 2013-06-18T13:06:43.383 回答
0

在 A 类:-

  ProgressBar progressBar; 
         @Override
        public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            setContentView(R.layout.main);
        progressBar = (ProgressBar) findViewById(R.id.progressBar1);


       ////Then Use the Reference on Progressbar 

       ClassB classb = new ClassB(this, progressBar);
    }

然后在 B 类:-

public class ClassB{

    private Context cnt;
    private ProgressBar progressBar;

    public ClassB(Context context, ProgressBar pBar){
         cnt = context;
         progressBar = pBar;

 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();


    }
于 2013-06-18T13:19:25.057 回答