2012-10-26 93 views
0

我似乎無法得到這個工作,在progressDialog完成後顯示如何顯示一個警告對話框?ProgressDialog關閉後顯示alertDialog

here'some相關代碼:

progressDialog = ProgressDialog.show(MainActivity.this, "", "Loading..."); 

     new Thread() { 
      public void run() { 
       try{ 
        initializer(); 
       } catch (Exception e) { 
        Log.e("tag", e.getMessage()); 
       } 
       progressDialog.dismiss(); 
       SharedPreferences welcome_pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this); 
       welcome = welcome_pref.getString("getwelcome", "null"); 

       if (welcome.equals("no")) { 

       }else{ 
        AlertDialog welcome = new AlertDialog.Builder(MainActivity.this).show(); 
        welcome.setContentView(R.layout.welcome); 
        welcome_pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this); 
        SharedPreferences.Editor welc = welcome_pref.edit(); 
        welc.putString("getwelcome", "no"); 
        welc.commit(); 
       } 
      } 
     }.start(); 
    } 

    private void initializer() { 
     startMoving = (ImageView) findViewById(R.id.imMove); 
     aboutApp = (ImageView) findViewById(R.id.imAbout); 
     devSite = (ImageView) findViewById(R.id.imDevSite); 
     movingTips = (ImageView) findViewById(R.id.imTips); 
     rateApp = (ImageView) findViewById(R.id.imRate); 

     start = (EditText) findViewById(R.id.etFrom); 
     end = (EditText) findViewById(R.id.etTo); 

     startMoving.setOnClickListener(this); 
     aboutApp.setOnClickListener(this); 
     devSite.setOnClickListener(this); 
     movingTips.setOnClickListener(this); 
     rateApp.setOnClickListener(this); 

     tvabout = (TextView) findViewById(R.id.tvabout); 
     tvstartmove = (TextView) findViewById(R.id.tvStart); 
     tvrate = (TextView) findViewById(R.id.tvRate); 
     tvdevelop = (TextView) findViewById(R.id.tvDev); 
     tvmovetips = (TextView) findViewById(R.id.tvTips); 

     tvabout.setShadowLayer(30, 0, 0, 0xff000000); 
     tvstartmove.setShadowLayer(30, 0, 0, 0xff000000); 
     tvrate.setShadowLayer(30, 0, 0, 0xff000000); 
     tvdevelop.setShadowLayer(30, 0, 0, 0xff000000); 
     tvmovetips.setShadowLayer(30, 0, 0, 0xff000000); 

     checklistitem(); 

    } 

    private void checklistitem() { 
     SharedPreferences createlistitem_pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this); 
     checkitem = createlistitem_pref.getString("getcheck", "null"); 

     if (checkitem.equals("no")) { 

     }else{ 
      SQLHandler checkitemstat = new SQLHandler(MainActivity.this); 
      checkitemstat.open(); 
      checkitemstat.createList(); 
      checkitemstat.close(); 
      SharedPreferences setitem_pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this); 
      SharedPreferences.Editor setcheck = setitem_pref.edit(); 
      setcheck.putString("getcheck", "no"); 
      setcheck.commit(); 
     } 
    } 
} 

我收到一個錯誤說Can't create handler inside thread that has not called Looper.prepare()我知道什麼造成這個錯誤,但我不知道如何解決這個問題。

這行代碼是造成這種情況的代碼。

   progressDialog.dismiss(); 
       SharedPreferences welcome_pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this); 
       welcome = welcome_pref.getString("getwelcome", "null"); 

       if (welcome.equals("no")) { 

       }else{ 
        AlertDialog welcome = new AlertDialog.Builder(MainActivity.this).show(); 
        welcome.setContentView(R.layout.welcome); 
        welcome_pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this); 
        SharedPreferences.Editor welc = welcome_pref.edit(); 
        welc.putString("getwelcome", "no"); 
        welc.commit(); 
       } 

回答

1

添加內runonUiThread您的警告對話框中顯示的代碼,

this.runOnUiThread(new Runnable() { 

       public void run() { 
        // TODO Auto-generated method stub 

       } 
      }); 

這樣的事情,

}else{ 

MainActivity.this.runOnUiThread(new Runnable() { 

       public void run() { 
         AlertDialog welcome = new AlertDialog.Builder(MainActivity.this).show(); 
      welcome.setContentView(R.layout.welcome); 
       } 
      }); 


        welcome_pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this); 
        SharedPreferences.Editor welc = welcome_pref.edit(); 
        welc.putString("getwelcome", "no"); 
        welc.commit(); 
       } 

作爲答案的一個建議這裏說,您無法從工作線程更新您的UI。當你更新任何用戶界面時你必須在UI線程中。

所以你需要使用處理程序或runonUiThread來做到這一點。但由於runOnUiThread內部使用處理程序,它應該可以幫助你。

+0

謝謝,完美的作品! :) – philip

0
SharedPreferences welcome_pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this); 
       welcome = welcome_pref.getString("getwelcome", "null"); 

       if (welcome.equals("no")) { 

       }else{ 
        AlertDialog welcome = new AlertDialog.Builder(MainActivity.this).show(); 
        welcome.setContentView(R.layout.welcome); 
        welcome_pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this); 
        SharedPreferences.Editor welc = welcome_pref.edit(); 
        welc.putString("getwelcome", "no"); 
        welc.commit(); 
       } 

Thread.

1

之外執行該代碼片段,您無法更新內部thread.Its可能只爲這你必須使用處理程序通過更新主線程的UI用戶界面。 要不然你可以像這樣

runOnUiThread(new Runnable() { 

       @Override 
       public void run() { 
        // TODO Auto-generated method stub 
        progressDialog.dismiss(); 
AlertDialog welcome = new AlertDialog.Builder(MainActivity.this).show(); 
        welcome.setContentView(R.layout.welcome); 
        welcome_pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this); 
        SharedPreferences.Editor welc = welcome_pref.edit(); 
        welc.putString("getwelcome", "no"); 
        welc.commit(); 
       } 
      });