0

这是问题所在,在此之前我将所有代码放在 onCreate(); 但加载时间超过 10 秒,它显示空白屏幕,直到屏幕完全加载。所以我把一些代码移到了doInBackground。但是我遇到了一些在 onCreate() 下从未发生过的问题,现在让我们看看代码:

String photoURL1;
@Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.app_details);

    tvDescription = (TextView)findViewById(R.id.tvDescription);

    tvTitle = (TextView)findViewById(R.id.tvTitle);
    tvDeveloper = (TextView)findViewById(R.id.tvDeveloper);

    rbRating = (RatingBar)findViewById(R.id.rbRating);

    ivLogo = (ImageView)findViewById(R.id.ivLogo);
    ivPhoto1 = (ImageView)findViewById(R.id.ivPhoto1);
    ivPhoto2 = (ImageView)findViewById(R.id.ivPhoto2);
    ivPhoto3 = (ImageView)findViewById(R.id.ivPhoto3);

    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... aurl) {
        SoapObject Request = new SoapObject (NAMESPACE, METHOD_NAME);

        Request.addProperty("title", getIntent().getExtras().getString("xdaTitle"));

        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();

            for(int i =0; i<resultString.getPropertyCount(); i++)
            {
                SoapObject array = (SoapObject) resultString .getProperty(i);

                title = array.getProperty(1).toString(); 
                developer = array.getProperty(3).toString();    

                         //load images      
                photoURL1 = array.getProperty(5).toString();   //get logo url


                BitmapFactory.Options bmOptions;
                bmOptions = new BitmapFactory.Options();
                bmOptions.inSampleSize = 1;         

            }
        }
        catch(Exception e){                                             
        }           

        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... progress2) {

    }


        @Override
        protected void onPostExecute(Void unused) {
            tvTitle.setText(title);
            tvDeveloper.setText(developer);
            BitmapFactory.Options bmOptions;
            bmOptions = new BitmapFactory.Options();
            bmOptions.inSampleSize = 1;

            Bitmap bmLogo = LoadImage(photoURL1, bmOptions);
            ivLogo.setImageBitmap(bmLogo);
            pdia.dismiss();
        }

这是问题所在。

  • 我的 tvTitle 只显示前 3 个字符,tvDeveloper 只显示 7~8 个字符(tvtitle 和 tvDeveloper 是一个 textView)
  • 如果我将图像加载器从 doInBackground 移动到 onPostExecute,则会发生强制关闭。

=======更新:我应该把这段代码停在哪里?==================

MyI = new Intent(Intent.ACTION_VIEW);
            MyI.setAction(android.content.Intent.ACTION_VIEW);
            MyI.setDataAndType(Uri.parse("file:///sdcard/MaxApps/" + apkURL.toString()), "application/vnd.android.package-archive");

            MyPI = PendingIntent.getActivity(getApplicationContext(), 0, MyI, 0);
            MyNM = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

            Intent intent = new Intent(getApplicationContext(), AppDetails.class);
            intent.putExtra("xdaTitle", title);
            final PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);

            notification = new Notification(R.drawable.logo, "Downloading...", System.currentTimeMillis());
            notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
            notification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.downloadapk);
            notification.contentIntent = pendingIntent;
            notification.contentView.setImageViewResource(R.id.imgIcon, R.drawable.save);
            notification.contentView.setTextViewText(R.id.tvText, "Downloading " + apkURL);
            notification.contentView.setViewVisibility(R.id.pbStatus, View.VISIBLE);
            notification.contentView.setProgressBar(R.id.pbStatus, 100, progress, false);        
            notificationManager = (NotificationManager) getApplicationContext().getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
4

2 回答 2

2

实际上,

ivLogo.setImageBitmap(bmLogo);
ivPhoto1.setImageBitmap(bmPhoto1);
ivPhoto2.setImageBitmap(bmPhoto2);
ivPhoto3.setImageBitmap(bmPhoto3);

这些行doInBackground()会导致问题。因为您正在尝试从工作线程(非 UI 线程)更新 MainUI 线程。

您不能从更新MainUIdoInBackground()更好地AsyncTask将此 UI 更新代码放入onPostExecute().AsyncTask

于 2012-07-06T06:32:02.883 回答
1

您不能在后台线程中更改 UI。

AsyncTask 有一个更好的选项来在下载时更新 UI,即使用publishProgress()

您需要像这样对代码进行一些更改才能实现它。

        @Override 
        protected Void doInBackground(String... aurl) { 

                for(int i =0; i<resultString.getPropertyCount(); i++) 
                {                    
                    String logo_URL = array.getProperty(5).toString();   //get logo url 
                    bmLogo = LoadImage(logo_URL, bmOptions); 
                    String photo1_URL = array.getProperty(6).toString(); 
                    bmPhoto1 = LoadImage(photo1_URL, bmOptions); 
                    String photo2_URL = array.getProperty(7).toString(); 
                    bmPhoto2 = LoadImage(photo2_URL, bmOptions); 
                    String photo3_URL = array.getProperty(8).toString(); 
                    bmPhoto3 = LoadImage(photo3_URL, bmOptions); 
                    publishProgress(null);
                } 
            } 
            catch(Exception e){                                              
            }            

            return null; 
        } 

        @Override 
        protected void onProgressUpdate(Integer... progress2) { 
            ivLogo.setImageBitmap(bmLogo); 
            ivPhoto1.setImageBitmap(bmPhoto1); 
            ivPhoto2.setImageBitmap(bmPhoto2); 
            ivPhoto3.setImageBitmap(bmPhoto3); 
        } 
于 2012-07-06T06:47:16.230 回答