Skip to content Skip to sidebar Skip to footer

Android: "thread Exiting With Uncaught Exception (group=0x4001d800)"

I'm doing a lan chat app bewteen android (client) and pc (java_server) both running on eclipse.The problem is android very untable and very often crash, however, its running very w

Solution 1:

I have solved the prolbem by adding a handler method:

privateHandler myHandler = newHandler(){
    @OverridepublicvoidhandleMessage(Message msg){
        String  str= (String) msg.obj;
        if(msg.what==1){
            msgArrayAdapter.add(str);
        }
    }
};`

and of course I have no idea why this works, but its just works.lol, thanks again for the help @Knickedi

Solution 2:

Here would be the clean way to do this (I don't know whether this solves your actual problem). But updating the adapter array from another thread is not a good idea anyway. And if your device is rotating than you would create another thread instead using the already running one. And don't forget to refresh your list by calling msgArrayAdapter.notifyDataSetChanged() when you changed the array (the messages). Here's the blueprint and idea...

Your activity. It will react on configuration changes and the actual destroy the right way:

private MessagePollThread thread;

publicvoidonCreate(Bundle b){
    super.onCreate(b);

    thread = (MessagePollThread) getLastNonConfigurationInstance();

    if (thread == null) {
        // activity runs for first time - create thread
        thread = newMessagePollThread();
        thread.activity = this;
        thread.start();
    } else {
        // just update the thread with the new activity - it's started already
        thread.activity = this;
    }
}

public Object onRetainNonConfigurationInstance(){
    // retain the thread for configuration changes e.g. orientation change (see onCreate)return thread;

    // you could also retain your old received messages here// just return new Object [] {thread, messageArray} and handle that in onCreate
}

publicvoidonDestroy(){
    super.onDestroy();

    if (isFinishing()) {
        // activity is about to destroy itself// shutdown the running thread
        thread.run = false;
        thread.interrupt(); // break the sleep
    }
}

The actual thread which handles requests (as inner static class of your activity):

privatestaticclassMessagePollThreadextendsThread {

    publicvolatilebooleanrun=true;
    publicvolatile Chat_client activity;
    privateHandlerhandler=newHandler();

    publicvoidrun() {

        // setup socket connection herewhile (run) {
            try {
                // you should / could also process send requests here// just use a synchronized list to check in your// activity (e.g. activity.messages) for new messages in queue// http://developer.android.com/reference/java/util/Collections.html#synchronizedList%28java.util.List%3CT%3E%29finalStringr=this.requestIn.readUTF();

                if (!run) {
                    // the thread could be shut down meanwhilecontinue;
                }

                handler.post(newRunnable() {
                    publicvoidrun() {
                        activity.handleResult(r);
                    }
                }
            } catch (IOException e) {
                handler.post(newRunnable() {
                    publicvoidrun() {
                        activity.finish();
                    }
                }
            }

            try {
                // sleep for a while// non stop polling will drain your battery very fast
                sleep(1000);
            } catch (InterruptException e) {
                // do nothing, just proceed
            }
        }

        // destroy socket connection here
    }
}

Post a Comment for "Android: "thread Exiting With Uncaught Exception (group=0x4001d800)""