Android looper.prepare()

时间:2011-09-07 20:53:11

标签: android android-asynctask

显示的代码使用AsyncTask类来调用阻塞进程,以便不阻止UI,并显示进度条。有趣的是onClick方法有效,但KeyCode方法没有。有什么建议吗?


    // inside the onCreate method of the activity
    .....
        ttsymbol.setOnKeyListener(new OnKeyListener() 
            { 
                @Override 
                public boolean onKey(View v, int keyCode, KeyEvent event) { 
                        switch (keyCode)
                        {
                        case KeyEvent.KEYCODE_ENTER:
                            setProgressBarIndeterminateVisibility(true);
                            new myBGtask().execute(ttsymbol.getText().toString().trim());
                            return true;
                        } 
                        return false; 
                } 
            }
        ); 
        mainPanel.addView(ttsymbol);

        //GetData button
        final Button btnGetData = new Button(this);
        // configure the button adding onClick method 
        btnGetData.setText("Get Pivot Point");
        btnGetData.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        btnGetData.setGravity(Gravity.CENTER);
        btnGetData.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                setProgressBarIndeterminateVisibility(true);
                new myBGtask().execute(ttsymbol.getText().toString().trim());
            }
        });
        mainPanel.addView(btnGetData);
    .....

    private class myBGtask extends AsyncTask {
    private String spage;
    protected Long  doInBackground(String... URL) {
        String starget;
      try {
          starget =  URL[0];
          spage = CustomHttpClient.myHttpGet(starget);
      } catch (Exception e) {
          Toast.makeText(getBaseContext(), "failed to GET webpage", Toast.LENGTH_SHORT).show();
          e.printStackTrace();
      }
      return (long) 0;
    }
    protected void onProgressUpdate(Integer... progress) {

    }
    protected void onPostExecute(Long result) {
        String sresults;
        setProgressBarIndeterminateVisibility(false);
        sresults = scrape(spage);
        if (sresults.length() == 0 ) 
        {
            lblDATE.setTextColor( Color.BLACK);
            lblDATE.setText( "cannot find that!");
            return;
        }
        String s[] = sresults.split("\n");

        lblDATE.setText(s[0]);
        .....
    }
    }

错误消息:


09-07 20:10:32.554: ERROR/AndroidRuntime(334): FATAL EXCEPTION: AsyncTask #1
09-07 20:10:32.554: ERROR/AndroidRuntime(334): java.lang.RuntimeException: An error occured while executing doInBackground()
09-07 20:10:32.554: ERROR/AndroidRuntime(334):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
09-07 20:10:32.554: ERROR/AndroidRuntime(334):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
09-07 20:10:32.554: ERROR/AndroidRuntime(334):     at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
09-07 20:10:32.554: ERROR/AndroidRuntime(334):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
09-07 20:10:32.554: ERROR/AndroidRuntime(334):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
09-07 20:10:32.554: ERROR/AndroidRuntime(334):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
09-07 20:10:32.554: ERROR/AndroidRuntime(334):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
09-07 20:10:32.554: ERROR/AndroidRuntime(334):     at java.lang.Thread.run(Thread.java:1019)
09-07 20:10:32.554: ERROR/AndroidRuntime(334): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
09-07 20:10:32.554: ERROR/AndroidRuntime(334):     at android.os.Handler.(Handler.java:121)
09-07 20:10:32.554: ERROR/AndroidRuntime(334):     at android.widget.Toast.(Toast.java:68)
09-07 20:10:32.554: ERROR/AndroidRuntime(334):     at android.widget.Toast.makeText(Toast.java:231)
09-07 20:10:32.554: ERROR/AndroidRuntime(334):     at com.chrisdev.geturltry1.Chrisgeturltry1Activity$myBGtask.doInBackground(Chrisgeturltry1Activity.java:204)
09-07 20:10:32.554: ERROR/AndroidRuntime(334):     at com.chrisdev.geturltry1.Chrisgeturltry1Activity$myBGtask.doInBackground(Chrisgeturltry1Activity.java:1)
09-07 20:10:32.554: ERROR/AndroidRuntime(334):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
09-07 20:10:32.554: ERROR/AndroidRuntime(334):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
09-07 20:10:32.554: ERROR/AndroidRuntime(334):     ... 4 more
09-07 20:10:32.619: WARN/ActivityManager(69):   Force finishing activity com.chrisdev.geturltry1/.Chrisgeturltry1Activity

2 个答案:

答案 0 :(得分:0)

您收到错误,因为从后台线程调用了Toast.makeToast()。如果您获得此特定值,则无法在doInBackground()中执行该操作,而是从此方法中返回描述异常的值并在onPostExecute()中显示toast。

显然,try块中的代码在一种情况下失败但在另一种情况下失败。这与looper.prepare()的问题不同。

答案 1 :(得分:0)

handler.post(showToast);

showToast(){  跑跑{     敬酒......  } }