2012-07-26 50 views
1

你好,我試圖讓我的TabHosts與進度對話框一起工作,但我不斷收到這些錯誤在各自的活動中加載ListView,但我不斷收到這些錯誤。試圖讓TabHost與進度對話框一起工作

代碼:

public class AndroidTabLayoutActivity extends TabActivity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     //final ProgressDialog dialog = ProgressDialog.show(getParent(), "Please wait..", "Doing stuff..", true); 
     final ProgressDialog pd = new ProgressDialog(this); 
     pd.setMessage("Please wait..."); 
     pd.show(); 
     Runnable myRun = new Runnable(){ 
     public void run(){ 
      Looper.myLooper().prepare(); 
      //DO EVERYTHING YOU WANT! 
      TabHost tabHost = getTabHost(); 

      // Tab for All 
      TabSpec allspec = tabHost.newTabSpec("All"); 
      allspec.setIndicator("All", getResources().getDrawable(R.drawable.icon_photos_tab)); 
      Intent allIntent = new Intent(AndroidTabLayoutActivity.this, AllApplicationsActivity.class);   
      allspec.setContent(allIntent); 

      // Tab for Installed Application 
      TabSpec systemspec = tabHost.newTabSpec("Installed Applications"); 
      // setting Title and Icon for the Tab 
      systemspec.setIndicator("Applications", getResources().getDrawable(R.drawable.icon_videos_tab)); 
      Intent systemIntent = new Intent(AndroidTabLayoutActivity.this, SystemApplicationsActivity.class); 
      systemspec.setContent(systemIntent); 


      // Adding all TabSpec to TabHost 
      tabHost.addTab(allspec); 
      tabHost.addTab(systemspec); 

      //Finally 
      runOnUiThread(new Runnable() { 
      public void run() { 


      pd.dismiss(); 
      } 
      }); 
     } 
     }; 

     Thread T = new Thread(myRun); 
     T.start(); 


    } 


} 

編輯:現在即時得到一個錯誤,當我啓動程序

07-26 17:55:05.637: E/AndroidRuntime(25603): FATAL EXCEPTION: Thread-974 
07-26 17:55:05.637: E/AndroidRuntime(25603): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 
07-26 17:55:05.637: E/AndroidRuntime(25603): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4286) 
07-26 17:55:05.637: E/AndroidRuntime(25603): at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:885) 
07-26 17:55:05.637: E/AndroidRuntime(25603): at android.view.View.requestLayout(View.java:12881) 
07-26 17:55:05.637: E/AndroidRuntime(25603): at android.view.View.requestLayout(View.java:12881) 
07-26 17:55:05.637: E/AndroidRuntime(25603): at android.view.View.requestLayout(View.java:12881) 
07-26 17:55:05.637: E/AndroidRuntime(25603): at android.view.View.requestLayout(View.java:12881) 
07-26 17:55:05.637: E/AndroidRuntime(25603): at android.view.View.requestLayout(View.java:12881) 
07-26 17:55:05.637: E/AndroidRuntime(25603): at android.view.View.requestLayout(View.java:12881) 
07-26 17:55:05.637: E/AndroidRuntime(25603): at android.view.ViewGroup.addView(ViewGroup.java:3211) 
07-26 17:55:05.637: E/AndroidRuntime(25603): at android.view.ViewGroup.addView(ViewGroup.java:3193) 
07-26 17:55:05.637: E/AndroidRuntime(25603): at android.widget.TabHost.setCurrentTab(TabHost.java:349) 
07-26 17:55:05.637: E/AndroidRuntime(25603): at android.widget.TabHost.addTab(TabHost.java:236) 
07-26 17:55:05.637: E/AndroidRuntime(25603): at com.example.androidtablayout.AndroidTabLayoutActivity$1.run(AndroidTabLayoutActivity.java:43) 
07-26 17:55:05.637: E/AndroidRuntime(25603): at java.lang.Thread.run(Thread.java:856) 
07-26 17:55:05.927: D/OpenGLRenderer(25603): Flushing caches (mode 0) 
07-26 17:55:05.967: D/OpenGLRenderer(25603): Flushing caches (mode 0) 
07-26 17:55:29.132: D/OpenGLRenderer(25603): Flushing caches (mode 2) 
07-26 17:55:29.372: D/AndroidRuntime(25603): Shutting down VM 

回答

0

替換:

Intent allIntent = new Intent(this, AllApplicationsActivity.class);    
Intent systemIntent = new Intent(this, SystemApplicationsActivity.class); 

有了:

Intent allIntent = new Intent(AndroidTabLayoutActivity.this, AllApplicationsActivity.class);   
Intent systemIntent = new Intent(AndroidTabLayoutActivity.this, SystemApplicationsActivity.class); 

更新第二次碰撞

你的ProgressDialog的草簽是不正確的。

ProgressDialog pd = new ProgressDialog(getContext()); 
pd.setMessage(...); 
pd.show(); 
+0

現在我的應用程序崩潰。 – dythe 2012-07-26 09:46:35

+0

更新了我的帖子.. – DroidBender 2012-07-26 09:51:30

+0

現在我可以看到只添加了「全部」選項卡並且加載了進度對話框,但是在加載完成後,我的ListView從AllApplicationsActivity/SystemApplicationsActivity不加載。 – dythe 2012-07-26 09:57:19