2015-05-19 38 views
0

有java類延伸FragmentActivity,最小SDK是10錯誤退出活動延伸FragmentActivity和包含ViewPager

我用ViewPager與FragmentManager以顯示viewpager指示器圖像

有時它顯示我錯誤時我要退出活動

private static ArrayList<PgItem> image_resource; 
public static ArrayList<ProjectItem> items = new ArrayList<ProjectItem>(); 
private static ViewPager v_pager; 
private static TitlePageIndicator pagerIndicator; 
public static FragmentManager fgmanger; 
public static void setSIAdapter(ArrayList<ProjectItem> inputArray , int ex){ 
    switch (ex){ 
     case 0: 
      Log.i(App.logs.WEBSERVICE, "CertificateAsync >> inputArray.size : " + inputArray.size()); 
      if(!inputArray.isEmpty()) { 
       items.clear(); 
       items.addAll(inputArray); 
       image_resource = new ArrayList<PgItem>(); 
       image_resource.clear(); 

       for (int i = 0; i < inputArray.size(); i++) { 
        String image = ""+inputArray.get(i).getPrjImage(); 
        if(image.trim().length() > 0 && image.replace(""+App.webServiceConfigs.ROOT , "").trim().length() > 0) { 
         String text = ""+inputArray.get(i).getPrjTitle(); 
         String ext = image.substring(image.lastIndexOf(".") , image.length()); 
         if(!image.toLowerCase().contains("_xl"+ext)){ 
          image = image.replace(ext , "_XL"+ext); 
         } 
         PgItem item = new PgItem(image, text); 
         image_resource.add(item); 
         Log.i(App.logs.TEST, "image_resource.image_name # " + (image_resource.size()-1) + " : " + image_resource.get(image_resource.size()-1).getImage()); 
        } 
       } 
       FragmentAdapter adapter = new FragmentAdapter(fgmanger , image_resource); 
       if(v_pager != null){ 
        v_pager.setAdapter(adapter); // Error Line 
        pagerIndicator.setViewPager(v_pager); 
        pagerIndicator.setTextSize(UIHelpers.textFont-2); 
        pagerIndicator.setSelectedBold(true); 
        pagerIndicator.setSelectedColor(Color.parseColor("#595458")); 
        pagerIndicator.setTextColor(Color.parseColor("#9E9E9E")); 
        pagerIndicator.setTypeface(Typeface.createFromAsset(App.configs.currentActivity.getAssets(), "yekan.ttf")); 
       } 
    //ImagePagerAdapterPg adapter = new ImagePagerAdapterPg(image_resource); 
    // v_pager.setAdapter(adapter); 

      } 
      break; 
     case -1: 
      Toast toastProtocol = Toast.makeText(App.configs.context , App.configs.context.getString(R.string.PROTOCOL_EXCEPTION) , Toast.LENGTH_LONG); 
      toastProtocol.setGravity(Gravity.CENTER, 0, (int) (UIHelpers.width * 0.25)); 
      toastProtocol.show(); 
      if(App.webServiceConfigs.connectionTimeOut <= 18000) { 
       App.webServiceConfigs.connectionTimeOut += 3000; 
      } 
      if(App.webServiceConfigs.socketTimeOut <= 18000) { 
       App.webServiceConfigs.socketTimeOut += 3000; 
      } 
      callWebservice(); 
      break; 
     case -2: 
      Toast toastIO = Toast.makeText(App.configs.context , App.configs.context.getString(R.string.IO_EXCEPTION) , Toast.LENGTH_LONG); 
      toastIO.setGravity(Gravity.CENTER, 0, (int) (UIHelpers.width * 0.25)); 
      toastIO.show(); 
      if(App.webServiceConfigs.connectionTimeOut <= 20000) { 
       App.webServiceConfigs.connectionTimeOut += 3000; 
      } 
      if(App.webServiceConfigs.socketTimeOut <= 20000) { 
       App.webServiceConfigs.socketTimeOut += 3000; 
      } 
      callWebservice(); 
      break; 
     case -3: 
      break; 
     case -4: 
      break; 
    } 
} 

我從緩存中加載圖像並顯示給用戶,然後連接到服務器並再次更新圖像適配器。

它爲我的錯誤:

05-19 17:56:27.213 9768-9768/com.emaarIt.app.Bernoulli E/AndroidRuntime﹕ FATAL EXCEPTION: main 
java.lang.IllegalStateException: Activity has been destroyed 
     at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1358) 
     at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595) 
     at android.support.v4.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java:578) 
     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:139) 
     at android.support.v4.view.ViewPager.setAdapter(ViewPager.java:415) 
     at com.emaarIt.app.Bernoulli.activities.ProjectGalleryActivity.setSIAdapter(ProjectGalleryActivity.java:225) 
     at com.emaarIt.app.Bernoulli.webservice.modules.ProjectGalleryAsync.onPostExecute(ProjectGalleryAsync.java:78) 
     at com.emaarIt.app.Bernoulli.webservice.modules.ProjectGalleryAsync.onPostExecute(ProjectGalleryAsync.java:32) 
     at android.os.AsyncTask.finish(AsyncTask.java:602) 
     at android.os.AsyncTask.access$600(AsyncTask.java:156) 
     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615) 
     at android.os.Handler.dispatchMessage(Handler.java:99) 
     at android.os.Looper.loop(Looper.java:137) 
     at android.app.ActivityThread.main(ActivityThread.java:4441) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:511) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
     at dalvik.system.NativeStart.main(Native Method) 

回答

3

原因:

這裏的問題是,你的活動得到了破壞之後,的AsyncTask還在工作,當它完成它的工作,它會調用的AsyncTask的OnPostExecute()回調方法,並且您正在設置與Activity聯繫的ViewPager的適配器,以將碎片添加到後端堆棧,並且因爲您的活動被破壞而無法發生。


建議的解決方案:

你可以抓住一個關於你的AsyncTask對象,並取消它的活動的的onDestroy()回調。

protected void onDestroy() { 
    super.onDestroy(); 
    // cancel the task 
    if(asyncTask != null && asyncTask.getStatus() != AsyncTask.Status.FINISHED) { 
     asyncTask.cancel(true); 
    } 
} 

什麼AsyncTask.cancel()做什麼?

調用此方法將在doInBackground(Object [])返回後在UI線程上調用onCancelled(Object) 。調用這個 方法可以保證不會調用onPostExecute(Object)。

+0

如果你這樣做,一定不要在'onCancelled'方法中觸及任何UI相關的東西,否則最終可能會遇到同樣的問題。 – Haspemulator