0

您好我正在尝试使用 AsyncTask 以随机间隔显示弹出消息。

    public class MainActivity extends Activity {
       boolean popped=false;
   @Override
        public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main); 
       new PopupTask().execute();
    }

仅当没有弹出其他弹出消息时,此方法才会弹出弹出消息。

  private class PopupTask extends AsyncTask<String, Void, String> {

      @Override
       protected String doInBackground(String... arg0) {

    Random r=new Random();
    int i1=r.nextInt();
    if(popped==false){
        LayoutInflater layoutInflater 
            = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);  
        View popupView = layoutInflater.inflate(R.layout.popup, null);  
       final PopupWindow popupWindow = new PopupWindow(
                   popupView, 
                   LayoutParams.MATCH_PARENT,  
                         LayoutParams.MATCH_PARENT);  

                if(!popped){
                    popupWindow.showAsDropDown(popupView, 90, 420);
                    popped=true;
                }

                 Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss);
                 btnDismiss.setOnClickListener(new Button.OnClickListener(){

                     @Override
                     public void onClick(View v) {
                      // TODO Auto-generated method stub
                      popupWindow.dismiss();
                      popped=false;

                     }});
    }
    return null;
}

但是,当控制到达 LayoutInflater 语句时,我得到一个异常。

有没有更好的方法来做到这一点?提前致谢

编辑:这是我的堆栈跟踪

04-25 02:40:33.298: E/AndroidRuntime(9724): FATAL EXCEPTION: AsyncTask #1
04-25 02:40:33.298: E/AndroidRuntime(9724): java.lang.RuntimeException: An error occured while executing doInBackground()
04-25 02:40:33.298: E/AndroidRuntime(9724):     at android.os.AsyncTask$3.done(AsyncTask.java:278)
04-25 02:40:33.298: E/AndroidRuntime(9724):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
04-25 02:40:33.298: E/AndroidRuntime(9724):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
04-25 02:40:33.298: E/AndroidRuntime(9724):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
04-25 02:40:33.298: E/AndroidRuntime(9724):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-25 02:40:33.298: E/AndroidRuntime(9724):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
04-25 02:40:33.298: E/AndroidRuntime(9724):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
04-25 02:40:33.298: E/AndroidRuntime(9724):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
04-25 02:40:33.298: E/AndroidRuntime(9724):     at java.lang.Thread.run(Thread.java:856)
04-25 02:40:33.298: E/AndroidRuntime(9724): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
04-25 02:40:33.298: E/AndroidRuntime(9724):     at android.os.Handler.<init>(Handler.java:121)
04-25 02:40:33.298: E/AndroidRuntime(9724):     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:329)
04-25 02:40:33.298: E/AndroidRuntime(9724):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:267)
04-25 02:40:33.298: E/AndroidRuntime(9724):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
04-25 02:40:33.298: E/AndroidRuntime(9724):     at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
04-25 02:40:33.298: E/AndroidRuntime(9724):     at android.view.Window$LocalWindowManager.addView(Window.java:537)
04-25 02:40:33.298: E/AndroidRuntime(9724):     at android.widget.PopupWindow.invokePopup(PopupWindow.java:988)
04-25 02:40:33.298: E/AndroidRuntime(9724):     at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:897)
04-25 02:40:33.298: E/AndroidRuntime(9724):     at com.example.popupsamp.MainActivity$PopupTask.doInBackground(MainActivity.java:85)
04-25 02:40:33.298: E/AndroidRuntime(9724):     at com.example.popupsamp.MainActivity$PopupTask.doInBackground(MainActivity.java:1)
04-25 02:40:33.298: E/AndroidRuntime(9724):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
04-25 02:40:33.298: E/AndroidRuntime(9724):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-25 02:40:33.298: E/AndroidRuntime(9724):     ... 5 more
4

2 回答 2

0

覆盖onProgressUpdate(); 并做这样的事情。

private class PopupTask extends AsyncTask<String, Integer, String>
 @Override
   protected String doInBackground(String... arg0) {

Random r=new Random();
int i1=r.nextInt();
if(popped==false){
   publishProgress(100); // Pass any Number
}

@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
if (values[0] == 100) {

LayoutInflater layoutInflater 
        = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);  
    View popupView = layoutInflater.inflate(R.layout.popup, null);  
   final PopupWindow popupWindow = new PopupWindow(
               popupView, 
               LayoutParams.MATCH_PARENT,  
                     LayoutParams.MATCH_PARENT); 

    }
}

另请阅读本教程以了解asyncTask

于 2013-04-25T04:50:33.877 回答
0

您不能在 UI 线程以外的线程中显示弹出窗口。将此代码移至 onPostExecute() 方法。

于 2013-04-24T21:45:22.517 回答