Android: java.lang.RuntimeException 由 Handler 引起

Android: java.lang.RuntimeException caused by Handler

早上好!

我在 google Play 商店中有一个 android 应用程序,它带有一个从服务器加载一些数据的小部件。 在我的 Play-Console 中,出现以下异常:

java.lang.RuntimeException: 


  at android.os.AsyncTask.done (AsyncTask.java:318)

  at java.util.concurrent.FutureTask.finishCompletion (FutureTask.java:354)

  at java.util.concurrent.FutureTask.setException (FutureTask.java:223)

  at java.util.concurrent.FutureTask.run (FutureTask.java:242)

  at android.os.AsyncTask$SerialExecutor.run (AsyncTask.java:243)

  at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1133)

  at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:607)

  at java.lang.Thread.run (Thread.java:762)


Caused by: java.lang.RuntimeException: 

  at android.os.Handler.<init> (Handler.java:200)

  at android.os.Handler.<init> (Handler.java:114)

  at package.Widget.fehler (Widget.java:279)

  at package.Widget_Server.doInBackground (Widget_Server.java:73)

  at package.Widget_Server.doInBackground (Widget_Server.java:19)

  at android.os.AsyncTask.call (AsyncTask.java:304)

  at java.util.concurrent.FutureTask.run (FutureTask.java:237)

java class "Widget_Server.java" 开始连接并从服务器获取数据。 "Widget.java" 是小部件的 Main_class。

Widget_Server.java,第 73 行:

finally {
     try {
        reader.close();
     } catch(Exception ex) {
               EXCEPTION (line 73) -> widget.fehler("Fehler! #2215", context);
     }
}

Widget.java,第 279 行:(方法内部 fehler(String, Context)

remoteViews.setTextViewText(R.id.label4, fehlerbeschreibung);

   EXCEPTION (line 279) -> Handler handler = new Handler();
   handler.postDelayed(new Runnable() {
      public void run() {
           remoteViews.setViewVisibility(R.id.loading, View. INVISIBLE);
           update(context);
      }
   }, 1000);

Handler 初始化如何抛出 runtime_exception?

我已经搜索了很长时间,但没有找到相关信息。

谢谢大家的回答!

Handler handler = new Handler() 实际上调用下面的构造函数。

public Handler(Callback callback, boolean async) {
    mLooper = Looper.myLooper();
    if (mLooper == null) {
        throw new RuntimeException(
            "Can't create handler inside thread that has not called Looper.prepare()");
    }
    mQueue = mLooper.mQueue;
    mCallback = callback;
    mAsynchronous = async;
}

如您所见,如果未调用 Looper.prepare(),则抛出 RuntimeException