Jiazai.javapackage com.jiazai;import android.app.Activity; 
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView;
public class Jiazai extends Activity
{
  private Button mButton1; 
  private TextView mTextView1; 
  public ProgressDialog myDialog = null; 
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) 
  { 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    
    mButton1 =(Button) findViewById(R.id.button);
    mTextView1 = (TextView) findViewById(R.id.textview);
    mButton1.setOnClickListener(myShowProgressBar); 
  } 
  Button.OnClickListener myShowProgressBar = 
    new Button.OnClickListener() 
  { 
    public void onClick(View arg0) 
    { 
      final CharSequence strDialogTitle = 
        getString(R.string.title);
      final CharSequence strDialogBody = 
        getString(R.string.body); 
      
      myDialog = ProgressDialog.show 
      ( Jiazai.this, 
          strDialogTitle, 
          strDialogBody, 
          true ); 
      mTextView1.setText(strDialogBody); 
      new Thread() 
      { 
        public void run() 
        { try 
        { 
          
          sleep(6000); 
          }
        catch (Exception e) 
        {
          e.printStackTrace(); 
        } finally
        { 
          myDialog.dismiss();
          }
        }
        }.start(); 
        }
    
  };
}
main.xml<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<Button android="@+id/button"
        android:layout_width="50sp"
        android:layout_height="wrap_content"
        android:text="Click me">
</Button>
<TextView android:id="@+id/textview"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:textSize="20sp">
</TextView>
</LinearLayout>
string.xml<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, Jiazai!</string>
    <string name="app_name">Jiazai</string>
    <string name="title">传奇世界</string>
    <string name="body">无限可能</string>
</resources>
一运行就出现 sorry 啥啥啥的

解决方案 »

  1.   

    可以把log贴出来看看。我没有运行代码,所以只是猜测:
    new Thread()  
      {  
      public void run()  
      { try  
      {  
        
      sleep(6000);  
      }
      catch (Exception e)  
      {
      e.printStackTrace();  
      } finally
      {  
      myDialog.dismiss();
      }
      }
      }.start(); 
    不要在你创建的新线程里dismiss dilog。在UI线程更新UI。
      

  2.   

    同意二楼,LZ的意思是弹出的对话框在6秒后自动结束。 这个可以用线程向主线程发消息来实现, 主线程收到消息后自己dismissDialog。 工作线程不能操作UI。