android - 致命异常 AsyncTask #1 和 Android doInBackground()

标签 android android-asynctask

早上好 StackOver Flow 专业人士,

我需要一点帮助。我正在运行一个 Android 应用程序,它通过 php 脚本调用 MySql 数据库,当我运行该应用程序时,我收到由空指针异常引起的 AsyncTask #1 的致命异常错误。它说它是在执行 doInBackground 时发生的。这是我从 MyBringBack 关注的教程。我真的需要全神贯注于此,因为几天来我一直在努力解决这个问题。如果我删除/评论大部分 Log.d 语句,它会运行正常,但我看不到 JSON 响应中正在发生的事情的标签,下面列出了代码,任何人都可以帮我解决这个问题或告诉我什么代码有问题。

这是logcat的错误日志

10-25 09:27:47.818: E/JSON Parser(1041): Error parsing data org.json.JSONException: Value array(2) of type java.lang.String cannot be converted to JSONObject 10-25 09:27:47.818: W/dalvikvm(1041): threadid=11: thread exiting with uncaught exception (group=0xb1b06ba8) 10-25 09:27:47.828: E/AndroidRuntime(1041): FATAL EXCEPTION: AsyncTask #1 10-25 09:27:47.828: E/AndroidRuntime(1041): Process: com.bobcatservice, PID: 1041 10-25 09:27:47.828: E/AndroidRuntime(1041): java.lang.RuntimeException: An error occured while executing doInBackground() 10-25 09:27:47.828: E/AndroidRuntime(1041): at android.os.AsyncTask$3.done(AsyncTask.java:300) 10-25 09:27:47.828: E/AndroidRuntime(1041): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) 10-25 09:27:47.828: E/AndroidRuntime(1041): at java.util.concurrent.FutureTask.setException(FutureTask.java:222) 10-25 09:27:47.828: E/AndroidRuntime(1041): at java.util.concurrent.FutureTask.run(FutureTask.java:242) 10-25 09:27:47.828: E/AndroidRuntime(1041): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 10-25 09:27:47.828: E/AndroidRuntime(1041): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 10-25 09:27:47.828: E/AndroidRuntime(1041): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 10-25 09:27:47.828: E/AndroidRuntime(1041): at java.lang.Thread.run(Thread.java:841) 10-25 09:27:47.828: E/AndroidRuntime(1041): Caused by: java.lang.NullPointerException 10-25 09:27:47.828: E/AndroidRuntime(1041): at com.bobcatservice.Login$AttemptLogin.doInBackground(Login.java:126) 10-25 09:27:47.828: E/AndroidRuntime(1041): at com.bobcatservice.Login$AttemptLogin.doInBackground(Login.java:1) 10-25 09:27:47.828: E/AndroidRuntime(1041): at android.os.AsyncTask$2.call(AsyncTask.java:288) 10-25 09:27:47.828: E/AndroidRuntime(1041): at java.util.concurrent.FutureTask.run(FutureTask.java:237) 10-25 09:27:47.828: E/AndroidRuntime(1041): ... 4 more 10-25 09:27:49.968: E/WindowManager(1041): android.view.WindowLeaked: Activity com.bobcatservice.Login has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{b1e8e430 V.E..... R......D 0,0-684,192} that was originally added here 10-25 09:27:49.968: E/WindowManager(1041): at android.view.ViewRootImpl.(ViewRootImpl.java:348) 10-25 09:27:49.968: E/WindowManager(1041): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248) 10-25 09:27:49.968: E/WindowManager(1041): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) 10-25 09:27:49.968: E/WindowManager(1041): at android.app.Dialog.show(Dialog.java:286)

这里是有问题的代码

//AsyncTask is a seperate thread than the thread that runs the GUI
//Any type of networking should be done with asynctask.
class AttemptLogin extends AsyncTask<String, String, String> {

     /**
    * Before starting background thread Show Progress Dialog
    * */
    boolean failure = false;

   @Override
   protected void onPreExecute() {
       super.onPreExecute();
       pDialog = new ProgressDialog(Login.this);
       pDialog.setMessage("Attempting login...");
       pDialog.setIndeterminate(false);
       pDialog.setCancelable(true);
       pDialog.show();
   }

    @Override
    protected String doInBackground(String... args) {
        // TODO Auto-generated method stub
         // Check for success tag
       int success;
       String username = user.getText().toString();
       String password = pass.getText().toString();

           // Building Parameters
           List<NameValuePair> params = new ArrayList<NameValuePair>();
           params.add(new BasicNameValuePair("username", username));
           params.add(new BasicNameValuePair("password", password));

           Log.d("request!", "starting");
           // getting product details by making HTTP request
           JSONObject json = jsonParser.makeHttpRequest(
                  LOGIN_URL, "POST", params);

           try {
           // check your log for json response
           Log.d("Login attempt", json.toString());

           // json success tag
           success = json.getInt(TAG_SUCCESS);
           if (success == 1) {
            Log.d("Login Successful!", json.toString());
            Intent i = new Intent(Login.this, Welcome.class);
            finish();
            startActivity(i);
            return json.getString(TAG_MESSAGE);
            }else{
            Log.d("Login Failure!", json.getString(TAG_MESSAGE));
            return json.getString(TAG_MESSAGE);

           }
        } catch (JSONException e) {
           e.printStackTrace();
       }

       return null;

    }
    /**
    * After completing background task Dismiss the progress dialog
    * **/
   protected void onPostExecute(String file_url) {
       // dismiss the dialog once product deleted
       pDialog.dismiss();
       if (file_url != null){
        Toast.makeText(Login.this, file_url, Toast.LENGTH_LONG).show();
       }

   }

}

最佳答案

If I remove/comment most of the Log.d statements it will run fine

我们似乎可以在 doInBackground() 方法中的一些日志调用中找到问题。这个如何?

Log.d("request!", "starting");

不,看起来不错。下一个呢?

Log.d("Login attempt", json.toString());

啊哈!我们如何获得这个 json 变量?

JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST", params);

检查你的 http 请求的结果,你可能在这里得到 null

如果 json 不为空怎么办?好吧,我们还有另一个可疑的日志调用:

Log.d("Login Failure!", json.getString(TAG_MESSAGE));

据我所知,如果 Log.d() 的第二个参数是 null,您将得到 NullPointerException,所以也检查这个。

如果这也不是问题怎么办?嗯,我建议你use debugger在这里。

关于android - 致命异常 AsyncTask #1 和 Android doInBackground(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26563046/

相关文章:

android - 如何更改计时器的格式?

Android layout_weight 没有按预期工作

java - 无法解析 de.measite.minidns :minidns:[0. 1,0.2)

android - RecyclerView 中的自定义列表项监听器界面不响应点击

android - 通过 findFragmentByTag 从 FragmentManager 获取 DialogFragment

android - AsyncTaskLoader 与 AsyncTask

Android:HttpURLConnection 抛出 EOFException

android - Android 市场应用程序的最大尺寸?

android - 在 recyclerview fragment Reandroid 中屏幕旋转后不显示进度条

java - Android 中是否可以链接异步任务?