0

这是代码,如果用户单击按钮,当前会显示进度对话框,并且“环”没有旋转。但是如果我将 progreedialog 的代码粘贴到 onCreate 下,一旦加载屏幕,“环”就会旋转。帮我看看哪里出错了。。

StaffChoice 类:

   public class StaffChoice extends Activity {

    @Override
protected void onCreate(Bundle savedInstanceState)
{                   
    super.onCreate(savedInstanceState);
    setContentView(R.layout.staffchoices);
}

public void onClickCategory(final View view)
{       
    final ProgressDialog progress=ProgressDialog.show(this, "Please wait", "Loading ...", true);

    new Thread()
    {
        public void run()
        {               
            Intent intent = new Intent(view.getContext(), Category.class);
            startActivity(intent);
            progress.dismiss();                     
        }
    }.start();
}
}

类别类中的 onCreate:

super.onCreate(savedInstanceState);
        setContentView(R.layout.category);
        final ListView lvCategory = (ListView) findViewById(R.id.lvCategory);

        SoapObject Request = new SoapObject (NAMESPACE, METHOD_NAME);
        SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

        soapEnvelope.dotNet = true;
        soapEnvelope.setOutputSoapObject(Request);

        AndroidHttpTransport aht = new AndroidHttpTransport(URL);

        try
        {
            aht.call(SOAP_ACTION, soapEnvelope);
            SoapObject resultString = (SoapObject) soapEnvelope.getResponse();

            final String[] strCategory = new String[resultString.getPropertyCount()];

            for(int i =0; i<resultString.getPropertyCount(); i++)
            {
                SoapObject array = (SoapObject) resultString .getProperty(i);
                strCategory[i] = array.getProperty(0).toString();   //get category
            }
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strCategory);
            lvCategory.setAdapter(adapter);

            lvCategory.setOnItemClickListener(new OnItemClickListener(){
                @Override
                public void onItemClick(AdapterView<?> arg0, final View arg1, int arg2, long arg3) {                    
                        Intent intent = new Intent(arg1.getContext(), CategoryGames.class);
                        startActivity(intent);
                }               
            });
        }

        catch(Exception e)
        {               
            String[] items = { "No Internet Connection, Please try again" };            
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
            lvCategory.setAdapter(adapter);
        }   
4

2 回答 2

1

您必须在线程运行时将任务或时间放入线程中,进度对话框正在显示,但是当任务/时间完成时,progressDialog 会关闭。

在 Android中使用AsynTask比使用 Thread 更好。

     new Thread()
            {
                public void run()

            {
                try{
                      Thread.sleep(10*1000);  //10 seconds

                   }catch(Exception e){
             } 
                handler.sendEmptyMessage(0);           
            }


           }.start();
于 2012-06-02T05:12:41.267 回答
1

而不是 Thread,我在新意图中使用 AsyncTask

    protected void onCreate(Bundle savedInstanceState)
    {
      new loadPage().execute(null, null, null);             
    }

    public class loadPage extends AsyncTask<String, Integer, Void> {

        private ProgressDialog pdia;

        @Override
        protected void onPreExecute(){
            super.onPreExecute();
            pdia = new ProgressDialog(AppDetails.this);
               pdia.setMessage("Loading...");
              pdia.show();
            }
        @Override
        protected Void doInBackground(String... arg0) {
        // TODO Auto-generated method stub
         }

        @Override
        protected void onPostExecute(Void unused) {
        pdia.dismiss();
         }
        }
于 2012-07-14T03:08:15.497 回答