Android Looper.prepare()

发布于 2024-12-03 08:28:13 字数 4786 浏览 0 评论 0原文

显示的代码使用 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

The code shown uses an AsyncTask class to call a blocking process so that the UI is not blocked, and a progressbar is shown. The funny thing is that the onClick method works, but the KeyCode method doesn't. Any suggestions please?


    // 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]);
        .....
    }
    }

error messages:


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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不爱素颜 2024-12-10 08:28:13

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

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

You get an error because Toast.makeToast() is called from background thread. You can't do it in doInBackground() instead return value from this method describing exception and show toast in onPostExecute() if you get this specific value.

Apparently, code in try block fails in one case but not another. That's different from problem with looper.prepare().

叹梦 2024-12-10 08:28:13

handler.post(showToast);

显示吐司(){
运行()。运行{
干杯……
}
}

handler.post(showToast);

showToast(){
run().run{
toast......
}
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文