开发者

Getting force close when i add view to other view when a thread is running

I am getting the below error

12-30 05:40:40.484: ERROR/AndroidRuntime(413): Uncaught handler: thread Thread-10 exiting due to uncaught exception
12-30 05:40:40.494: ERROR/AndroidRuntime(413): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
12-30 05:40:40.494: ERROR/AndroidRuntime(413):     at android.view.ViewRoot.checkThread(ViewRoot.java:2629)
12-30 05:40:40.494: ERROR/AndroidRuntime(413):     at android.view.ViewRoot.requestLayout(ViewRoot.java:545)
12-30 05:40:40.494: ERROR/AndroidRuntime(413):     at android.view.View.requestLayout(View.java:7657)
12-30 05:40:40.494: ERROR/AndroidRuntime(413):     at android.view.View.requestLayout(View.java:7657)
12-30 05:40:40.494: ERROR/AndroidRuntime(413):     at android.view.View.requestLayout(View.java:7657)
12-30 05:40:40.494: ERROR/AndroidRuntime(413):     at android.view.View.requestLayout(View.java:7657)
12-3开发者_开发知识库0 05:40:40.494: ERROR/AndroidRuntime(413):     at android.view.View.requestLayout(View.java:7657)
12-30 05:40:40.494: ERROR/AndroidRuntime(413):     at android.view.ViewGroup.addView(ViewGroup.java:1749)
12-30 05:40:40.494: ERROR/AndroidRuntime(413):     at android.view.ViewGroup.addView(ViewGroup.java:1708)
12-30 05:40:40.494: ERROR/AndroidRuntime(413):     at android.view.ViewGroup.addView(ViewGroup.java:1688)
12-30 05:40:40.494: ERROR/AndroidRuntime(413):     at com.wwwww.shout.presentationLayer.Shout$1.run(Shout.java:137)

and my code is

myProgressDialog = ProgressDialog.show(Shout.this,"","Loading...",true);

         new Thread() 
         {
         public void run()
         {
             String xml;



                 xml="<spGetUserMessages><SearchLocation></SearchLocation></spGetUserMessages>";
                 messages =parse.GetGetUserMessages(dataparsing.ILGetUserMessages(xml));
        myProgressDialog.dismiss();
((LinearLayout)findViewById(R.id.LinearlayoutMessage)).addView(iiii);


      }

    }.start();

at the time of adding views to the layout i am getting the above error.what is the wrong in this.Please give me some suggestions.Thanks in advance


You can use Handlers as in this example(handleMessage runs on UI thread, so there you can modify views) or for more sophisticated things, use AsyncTask (onPostExecute runs also in UI thread)

That's how your code should look like using Handler:

private Handler myHandler = new Handler() {
    public void handleMessage(Message msg) {
        //in case you passed data to message (see below) here you can retrieve it
        myProgressDialog.dismiss();
        ((LinearLayout)findViewById(R.id.LinearlayoutMessage)).addView(iiii);
    }
};

private myProgressDialog = ProgressDialog.show(Shout.this,"","Loading...",true);

new Thread() {
    public void run() {
        String xml;
        xml="<spGetUserMessages><SearchLocation></SearchLocation></spGetUserMessages>";
        messages = parse.GetGetUserMessages(dataparsing.ILGetUserMessages(xml));
        Message msg = myHandler.obtainMessage();
        //here you can add data to the message using msg.obj, msg.what...
        //refer to http://developer.android.com/reference/android/os/Handler.html for details
        myHandler.sendMessage(msg);
   }
}.start();


Only the UI thread can modify views.

Use Activity.runOnUIThread(Runnable)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜