开发者

Thread problem in android?

Consider i have one thread as a separate class , for example SimpleThread.java,

class SimpleThread extends Thread {

  public SimpleThread(String str) {
    super(str);
    }
    public void run() {
    for (int i = 0; i < 10; i++) {
        System.out.println(i + " " + getName()开发者_如何学Go);
            try {
        sleep((int)(Math.random() * 1000));
        } catch (InterruptedException e) {}
    }
    System.out.println("DONE! " + getName());
    }
}

from my android home.java i need to start the thread,

new SimpleThread("Jamaica").start();

once the loop end i need to shoe the alert,but when i use

 authalert = new AlertDialog.Builder(this);

it shows null pointer execption, i need a context over here in thread class , is there any other way to do this.


Hey you should use Handler for this here is the code ...

ProgressDialog _progressDialog = ProgressDialog.show(this,"Saving Data","Please wait......");
settintAdater();

 private void settingAdater(){

        Thread _thread = new Thread(){

            public void run() {

                Message _msg = new Message();
                _msg.what = 1; 
                 // Do your task where you want to rerieve data to set in adapet
                YourCalss.this._handle.sendMessage(_msg);
            };
        };
        _thread.start();
    }
 Handler _handle = new Handler(){

        public void handleMessage(Message msg) {

            switch(msg.what){

                case 1:
                    _progressDialog.dismiss();
                     listview.setAdapter();
            }
        }
 }


One way of solving your problem is using Handlers, as Sujit suggested. Other way is using AsyncTask. Read here.


the problem is : when you launch the thread, the Compiler will not wait until the thread finish his treatement , he will execute the next instruction ( authalert = new AlertDialog.Builder(this); )

so there are two or three ways to do this :

1) , use handler

2) define your own listener for your thread in order to listen until he finished his treatement ,

3) you can pass the Context of your activity , and at the last line of your run method , display the AlertDialog ( with Activity.runOnUiThread(new Runnable); )


You should read http://www.aviyehuda.com/2010/12/android-multithreading-in-a-ui-environment/ and http://developer.android.com/resources/articles/painless-threading.html

one way would be put a handler in your calling activity:

final mContext=this;
final Handler mHandler=new Handler(){

@Override
public void handleMessage(Message msg) {
    int yourIntReturnValue=msg.what;
    //cast your object back to whatever it was lets say it was a string:
    // String yourString=(String) msg.obj;
    //do something like authalert = new AlertDialog.Builder(mContext);


}
};

then

class SimpleThread extends Thread {
   Handler mHandler;

public SimpleThread(String str, Handler h) { 

super(str);
mHandler=h;
 } 
public void run() { 
for (int i = 0; i < 10; i++) { 
System.out.println(i + " " + getName()); 
try { 
sleep((int)(Math.random() * 1000)); 
} catch (InterruptedException e) {} 
} 
System.out.println("DONE! " + getName()); 
 Message.obtain(mHandler, someIntRetValue,
                "DONE"  ).sendToTarget();


}
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜