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

AsyncTask#1和Android doInBackground()的致命异常,android,android-asynctask,Android,Android Asynctask,早上好,StackOver Flow Pros 我需要一点帮助。我正在运行一个Android应用程序,它通过一个php脚本调用MySql数据库,当我运行该应用程序时,我得到一个AsyncTask#1的致命异常错误,该错误是由空指针异常引起的。它说这是在执行doInBackground时发生的。这是我在MyBringBack中遵循的教程。我真的需要好好想想,因为我已经想了好几天了。如果我删除/注释大多数Log.d语句,它将正常运行,但我不会从JSON响应中看到正在发生的事情的标记,下面列出了代码,

早上好,StackOver Flow Pros

我需要一点帮助。我正在运行一个Android应用程序,它通过一个php脚本调用MySql数据库,当我运行该应用程序时,我得到一个AsyncTask#1的致命异常错误,该错误是由空指针异常引起的。它说这是在执行doInBackground时发生的。这是我在MyBringBack中遵循的教程。我真的需要好好想想,因为我已经想了好几天了。如果我删除/注释大多数Log.d语句,它将正常运行,但我不会从JSON响应中看到正在发生的事情的标记,下面列出了代码,有人能帮我解决这个问题或告诉我代码有什么问题吗

这是logcat的错误日志

10-25 09:27:47.818:E/JSON解析器(1041):解析数据org.JSON.JSONException时出错:java.lang.String类型的值数组(2)无法转换为JSONObject 10-25 09:27:47.818:W/dalvikvm(1041):threadid=11:线程退出时出现未捕获异常(组=0xb1b06ba8) 10-25 09:27:47.828:E/AndroidRuntime(1041):致命异常:AsyncTask#1 10-25 09:27:47.828:E/AndroidRuntime(1041):进程:com.bobcatservice,PID:1041 10-25 09:27:47.828:E/AndroidRuntime(1041):java.lang.RuntimeException:执行doInBackground()时出错 10-25 09:27:47.828:E/AndroidRuntime(1041):在android.os.AsyncTask$3.done(AsyncTask.java:300) 10-25 09:27:47.828:E/AndroidRuntime(1041):在java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) 10-25 09:27:47.828:E/AndroidRuntime(1041):在java.util.concurrent.FutureTask.setException(FutureTask.java:222) 10-25 09:27:47.828:E/AndroidRuntime(1041):在java.util.concurrent.FutureTask.run(FutureTask.java:242) 10-25 09:27:47.828:E/AndroidRuntime(1041):在android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 10-25 09:27:47.828:E/AndroidRuntime(1041):位于java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 10-25 09:27:47.828:E/AndroidRuntime(1041):在java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 10-25 09:27:47.828:E/AndroidRuntime(1041):在java.lang.Thread.run(Thread.java:841) 10-25 09:27:47.828:E/AndroidRuntime(1041):由以下原因引起:java.lang.NullPointerException 10-25 09:27:47.828:E/AndroidRuntime(1041):在com.bobcatservice.Login$AttemptLogin.doInBackground(Login.java:126) 10-25 09:27:47.828:E/AndroidRuntime(1041):在com.bobcatservice.Login$AttemptLogin.doInBackground(Login.java:1) 10-25 09:27:47.828:E/AndroidRuntime(1041):在android.os.AsyncTask$2.call(AsyncTask.java:288) 10-25 09:27:47.828:E/AndroidRuntime(1041):在java.util.concurrent.FutureTask.run(FutureTask.java:237) 10-25 09:27:47.828:E/AndroidRuntime(1041):。。。4更多 10-25 09:27:49.968:E/WindowManager(1041):android.view.WindowLeaked:Activity com.bobcatservice.Login泄漏了window com.android.internal.policy.impl.PhoneWindow$DecorView{b1e8e430 V.E…..R……D 0,0-684192},最初添加到这里 10-25 09:27:49.968:E/WindowManager(1041):在android.view.ViewRootImpl。(ViewRootImpl.java:348) 10-25 09:27:49.968:E/WindowManager(1041):在android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248) 10-25 09:27:49.968:E/WindowManager(1041):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) 10-25 09:27:49.968:E/WindowManager(1041):在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();
       }

   }

}
//AsyncTask是一个独立于运行GUI的线程
//任何类型的网络都应该使用asynctask完成。
类AttemptLogin扩展了AsyncTask{
/**
*在启动后台线程显示进度对话框之前
* */
布尔失败=假;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=newprogressdialog(Login.this);
setMessage(“正在尝试登录…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();
}
@凌驾
受保护的字符串doInBackground(字符串…args){
//TODO自动生成的方法存根
//检查成功标签
成功;
字符串username=user.getText().toString();
字符串密码=pass.getText().toString();
//建筑参数
List params=new ArrayList();
添加(新的BasicNameValuePair(“用户名”,用户名));
添加(新的BasicNameValuePair(“密码”,password));
Log.d(“请求!”,“启动”);
//通过发出HTTP请求获取产品详细信息
JSONObject json=jsonParser.makeHttpRequest(
登录URL,“POST”,参数);
试一试{
//检查日志中的json响应
Log.d(“登录尝试”,json.toString());
//json成功标记
success=json.getInt(TAG_success);
如果(成功==1){
Log.d(“登录成功!”,json.toString();
Intent i=新Intent(Login.this、Welcome.class);
完成();
星触觉(i);
返回json.getString(TAG_消息);
}否则{
Log.d(“登录失败!”,json.getString(TAG_MESSAGE));
返回json.getString(TAG_消息);
}
}捕获(JSONException e){
e、 printStackTrace();
}
返回null;
}
/**
*完成后台任务后,关闭“进度”对话框
* **/
受保护的void onPostExecute(字符串文件\u url){
//删除产品后关闭对话框
pDialog.disclose();
如果(文件url!=null){
Toast.makeText(Login.this,file_url,Toast.LENGTH_LONG.show();
}
}
}
}

如果我删除/注释大多数Log.d语句,它将正常运行

好像是w
Log.d("request!", "starting");
Log.d("Login attempt", json.toString());
JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST", params);
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
// url to create send data. This contains the ip address of my machine on which the local server is running. You will write the IP address of your machine
         private static String url = "http://192.168.***.1:8080/android/create_product.php";
<?php 
// array for JSON response
$response = array();
// check for required fields
if (isset($_POST['id']) && isset($_POST['name'])) { 
$id = $_POST['id'];
$name = $_POST['name'];   
// include db connect class
require_once __DIR__ . '/db_connect.php'; 
// connecting to db
$db = new DB_CONNECT(); 
// mysql inserting a new row
$result = mysql_query("INSERT INTO table_name(id, name) VALUES('$id', '$name')"); 
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Product successfully created."; 
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] =  mysql_error();; 
// echoing JSON response
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing"; 
// echoing JSON response
echo json_encode($response);
}
?>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />