2010-06-09 46 views
0

我用下面的从服务器 服务器端获取数据的WebServices检索结果ksoap2黑屏,而从符在android系统

whenever activity start, onCreate i am using spinner for displying data returned by the webservices 
when this activity start it showing black screen after lunching the activity .i found black screen is coming when activity connecting to webservices 

How to resolve this 


MyCode 

public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
try { 
       //Display the online and busy people display in spinner 
       //people are display in relative people only(Mygroup) 
       /* get the online and busy people who are in user group from DB*/ 
       users_names_ids=new ParseXMLString().convertusernames(new DataParsingComm().ILGetOnlinePeoples("<spGetOnlinePeoples><UserID>"+GetCurrentUserID.id+"</UserID></spGetOnlinePeoples>")); 

       /* create an array with the size of number of peoples whose status is online or busy */ 
       String[] array =new String[users_names_ids.size()];   
       int setselction=0;// initialize the selection to 0. 

       /* if array length is greater than zero, that means getting at least one person whose status is online or busy */ 
       if(array.length>0){ 

        /* Returns an enumeration on the keys of this Hashtable instance. And assigns into Enumeration instance variable */ 
        Enumeration e= users_names_ids.keys(); 

        /* Iterate list Enumeration until it does't has any more elements */ 
        for(int i=0;e.hasMoreElements();i++) 
         try{ 
          /* get all persons names into the array list */ 
          array[i]=e.nextElement().toString(); 

          /* Get the ChatUserName value from the ChatInPeopleDetails preferences. And If it is in this list set selection to the index 'i' */ 
          if(getSharedPreferences("ChatInPeopleDetails", 0).getString("ChatUserName", "").equals(array[i])) 
           setselction=i; 

           /* 
           * Get the String value of Relname, that previously added with putExtra() as extended data to the parent intent 
           * If that value is not null and exists in the array list then 
           * set the selection to the index 'i'. 
           * */ 
          else if(getIntent().getStringExtra("Relname")!=null && getIntent().getStringExtra("Relname").equals(array[i])) 
             setselction=i; 

         }catch(Exception ex){ 
          ex.printStackTrace(); 
         } 
         finally 
         { 
          System.gc(); 
          System.runFinalization(); 
         } 
       } 

       /* create a new array adapter with the ChatForm context and array objects */ 
       ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(ChatForm.this,android.R.layout.simple_spinner_item, array); 

       /* Set the layout resource to create the drop down views. */ 
       adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

       /* The Adapter is used to provide the data which backs this Spinner SpinnerUsersToChat. */ 
       ((Spinner)findViewById(R.id.SpinnerUsersToChat)).setAdapter(adapter2); 

       /* Get the ChatUserName value from the ChatInPeopleDetails preferences. If this value is not null*/ 
       if(getSharedPreferences("ChatInPeopleDetails", 0).getString("ChatUserName", "") !=null) 
       { 
        /* Set the currently selected item of spinner based on selection variable value */ 
        ((Spinner)findViewById(R.id.SpinnerUsersToChat)).setSelection(setselction); 
       } 

       /* Register a callback to be invoked when an item in this AdapterView has been selected.*/ 
       ((Spinner)findViewById(R.id.SpinnerUsersToChat)).setOnItemSelectedListener(new OnItemSelectedListener() 
       { 
        public void onItemSelected(AdapterView<?> parent,View v,int position,long id) 
         {     
          /* call getMsg() to get messages and display them*/ 
          getMsg(); 
          /* Causes the Runnable to be added to the message queue. The runnable will be run on the user interface thread.*/ 
          ((ScrollView)findViewById(R.id.ScrollView06)).post(new Runnable() 
           { 
           public void run() 
           { 
            /* This fullScroll() method will scroll the view to the bottom .*/ 
            ((ScrollView)findViewById(R.id.ScrollView06)).fullScroll(View.FOCUS_DOWN); 
           } 

          }); 
         } 
         /* on nothing selected to do somthing . this an overridden method */ 
         public void onNothingSelected(AdapterView<?> arg0) { 

         } 

       }); 

      } catch (Exception e1) { 
       e1.printStackTrace(); 
      } 

}

+0

你能请张贴您的代码 – 2010-06-09 11:49:31

+0

您是否在UI线程上执行此webservice调用? – 2010-06-09 11:59:57

+0

嗨米娜和Rpond .i发布了代码并分享您的想法 – Aswan 2010-06-10 04:48:05

回答

1

听起来像是你试图在主UI线程上执行您的请求。因为Web请求需要时间,所以UI线程必须等到完成之后才能更新屏幕。

您应该使用的AsyncTask做背景这个请求:

class WebRequestTask extends AsyncTask<String, int[], String[]> { 

@Override 
protected String[] doInBackground(String... params) { 
    String[] results = null; 

    // Anything done here is in a seperate thread to the UI thread 
    // Do you download from here 

    // If you want to update the progress you can call 
    publishProgress(int progress); // This passes to the onProgressUpdate method 

    return results; // This passes the result to the onPostExecute method 
} 

@Override 
protected void onProgressUpdate(Integer... progress) { 
    // This is on your UI thread, useful if you have a progressbar in your view 
} 

@Override 
protected void onPostExecute(String[] results) { 
    super.onPostExecute(results); 
    // This is back on your UI thread - 
    } 
} 

new WebRequestTask().execute(); 

看一看http://developer.android.com/reference/android/os/AsyncTask.html更多信息启动您的AsyncTask

+0

Thankq对您的回复我将立即尝试 – Aswan 2010-06-10 04:48:40