2012-04-08 25 views
0

以下是我的代码:基本上我打电话进步类从一个按钮监听器运行它,我得到这个错误我试着打电话从按钮事件的私有类,我也得到了java.lang.RuntimeException:

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 

是thiere从按钮或菜单运行此进度类的方法吗?

public class ProgressDialogeActivity extends Activity { 
private ProgressDialog dialog; 
int count = 0; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.main); 

    // running from main activity 
    dialog = new ProgressDialog(this); 
    dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
    dialog.setMessage("Deleteng Contacts ..."); 
    dialog.setMax(50); 
    // dialog.show(); 
    // dialog.setProgress(40); 
    Object Maxmssg = checkhowmanysms(); 
    // dialog.setMax((Integer) Maxmssg); 

    Button deletesms = (Button) findViewById(R.id.button1); 
    deletesms.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      progress prog = new progress(); 
      prog.start(); 

     } 
    }); 

} 

public class progress extends Thread { 
    @Override 
    public void run() { 

     dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
     dialog.setMessage("Deleteng Contacts ..."); 
     int size = checkhowmanysms(); 
     Object Maxmssg = checkhowmanysms(); 
     dialog.setMax((Integer) Maxmssg); 
     dialog.show(); 

     Uri inboxUri = Uri.parse("content://sms"); 
     Cursor item = getContentResolver().query(inboxUri, null, null, 
       null, null); 
     while (item.moveToNext()) { 
      count++; 
      dialog.setProgress(count); 

      // Delete the SMS 
      String pid = item.getString(0); // Get id; 
      String uri = "content://sms"; 
      getContentResolver().delete(Uri.parse(uri), null, null); 

     } 
     dialog.dismiss(); 

    } 

} 
+0

您是否添加了正确的权限? [许可] [1] [1]:http://stackoverflow.com/questions/9616476/delete-sms-from-smstable-in-android – Oglop 2012-04-08 19:41:11

回答

0

您正在尝试从后台线程修改UI,你不能这样做,而是使用HandlerAsyncTask(对于UI修改的部分)。

0

问题是您正在尝试从非UI线程更改UI。解决方法是使用Handlerpost()方法之一,或者如果您可以调用Activity的方法runOnUiThread()-方法

相关问题