致命异常:AsyncTask#1

时间:2017-02-07 11:13:00

标签: android

我已经搜索了其他类似的问题,但没有一个对我有用,所以我想问你们。

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.push registerUserNotificationSettings];
[appDelegate initSinchClient:[saveDefault objectForKey:@"emailId"]];
[appDelegate RegisterPushNotification];

这是我的代码

  02-07 18:39:03.953 6252-6306/com.example.jl.httpsample
       E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
  Process: com.example.jl.httpsample, PID: 6252
  java.lang.RuntimeException: An error occured while executing doInBackground()
  at android.os.AsyncTask$3.done(AsyncTask.java:300)
  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
  at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
  at java.util.concurrent.FutureTask.run(FutureTask.java:242)
  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
  at java.lang.Thread.run(Thread.java:841)
  Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
  at android.os.Handler.<init>(Handler.java:200)
  at android.os.Handler.<init>(Handler.java:114)
  at android.widget.Toast$TN.<init>(Toast.java:372)
  at android.widget.Toast.<init>(Toast.java:105)
  at android.widget.Toast.makeText(Toast.java:264)
  at com.example.jl.httpsample.MainActivity$1GetJSON.doInBackground(MainActivity.java:70)
  at com.example.jl.httpsample.MainActivity$1GetJSON.doInBackground(MainActivity.java:45)
  at android.os.AsyncTask$2.call(AsyncTask.java:288)
  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
  at java.lang.Thread.run(Thread.java:841) 

有人提到错误来自doInBackground(),但我是Android开发的新手,所以我很难理解发生了什么。

2 个答案:

答案 0 :(得分:0)

问题在于

catch (Exception e){          
    Toast.makeText(getBaseContext(),e.getMessage(),Toast.LENGTH_LONG).show();
    return null;
 }

Toast需要在UI线程上运行,违反了doInBackgroun在工作线程中运行的策略。处理onPostExecute中的错误案例。

答案 1 :(得分:0)

请使用Jsonparser文件来运行此类doInBackground方法。有关JsonParser类的更多信息,请参阅以下链接。

How to implement Login with HttpURLConnection and PHP server in Android