致命异常:传入/传出呼叫期间的AsyncTask#2

时间:2015-03-03 08:05:24

标签: android

场景:应用程序在后台。在接听或拨出电话期间,我会显示带有两个按钮的alertview。 。 “显示和取消”。当我点击“显示”按钮,然后这个代码工作。但我的申请崩溃了。任何人都可以告诉我原因。

final class sendserverhit extends AsyncTask< Void,Void, Void>

    {

  @Override
  protected void onPostExecute(Void result) {

   // TODO Auto-generated method stub
    Toast.makeText(CallHelper.this, "status : "+status + " message : "+message+" applidation :"+application+" data1 : "+data, Toast.LENGTH_LONG).show();


   super.onPostExecute(result);
  }

  @Override
  protected Void doInBackground(Void... params) {
   // TODO Auto-generated method stub
            try {
    GetText();
   } catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   return null;
  }

    }

这是我的GetText代码

public  void  GetText()  throws  UnsupportedEncodingException
    {


         // Create data variable for sent values to server  
          String data = URLEncoder.encode("client_phone", "UTF-8") 
                       + "=" + URLEncoder.encode(numberin, "UTF-8"); 
          data += "&" + URLEncoder.encode("user_phone", "UTF-8") 
                  + "=" + URLEncoder.encode("1001", "UTF-8");
          data += "&" + URLEncoder.encode("call_type", "UTF-8") 
                  + "=" + URLEncoder.encode("2", "UTF-8");
          data += "&" + URLEncoder.encode("identity", "UTF-8") 
                      + "=" + URLEncoder.encode("Null", "UTF-8");
          data += "&" + URLEncoder.encode("comments", "UTF-8") 
                  + "=" + URLEncoder.encode("", "UTF-8");

          String text = "";
          BufferedReader reader=null;


          // Send data 
        try
        { 


            // Defined URL  where to send data
            URL url = new URL("http://..iescrm/push_data?");

         // Send POST data request

          URLConnection conn = url.openConnection(); 
          conn.setDoOutput(true); 
          OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); 
          wr.write( data );

          wr.flush(); 

          // Get the server response 

        reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String line = null;

        // Read Server Response
        while((line = reader.readLine()) != null)
            {
                   // Append server response in string
                   sb.append(line + "\n");
            }


           text = sb.toString();


        }
        catch(Exception ex)
        {
             ex.printStackTrace();
        }
        finally
        {
            try
            {

                reader.close();
                JSONObject jObject = new JSONObject(text);
                  status = jObject.getString("status");
 //{"status":true,"message":"success","user_phone_number":"1001","identity":null}
                //  {"application":"SkiesCRM","status":false,"message":"Invalid identity","data":""}
  message = jObject.getString("message");

                  application = jObject.getString("application");
                  data1 = jObject.getString("data");


             }

            catch(Exception ex) { Toast.makeText(ctx, "status : ", Toast.LENGTH_LONG).show();}
        }
        // Show response on activity

    }

这是我的错误日志

03-03 12:37:00.999: E/AndroidRuntime(21166): FATAL EXCEPTION: AsyncTask #2
03-03 12:37:00.999: E/AndroidRuntime(21166): java.lang.RuntimeException: An error occured while executing doInBackground()
03-03 12:37:00.999: E/AndroidRuntime(21166):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
03-03 12:37:00.999: E/AndroidRuntime(21166):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
03-03 12:37:00.999: E/AndroidRuntime(21166):    at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
03-03 12:37:00.999: E/AndroidRuntime(21166):    at java.util.concurrent.FutureTask.run(FutureTask.java:239)
03-03 12:37:00.999: E/AndroidRuntime(21166):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
03-03 12:37:00.999: E/AndroidRuntime(21166):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
03-03 12:37:00.999: E/AndroidRuntime(21166):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
03-03 12:37:00.999: E/AndroidRuntime(21166):    at java.lang.Thread.run(Thread.java:838)
03-03 12:37:00.999: E/AndroidRuntime(21166): Caused by: java.lang.NullPointerException
03-03 12:37:00.999: E/AndroidRuntime(21166):    at libcore.net.UriCodec.encode(UriCodec.java:132)
03-03 12:37:00.999: E/AndroidRuntime(21166):    at java.net.URLEncoder.encode(URLEncoder.java:57)
03-03 12:37:00.999: E/AndroidRuntime(21166):    at com.bitgriff.androidcalls.CallHelper.GetText(CallHelper.java:153)
03-03 12:37:00.999: E/AndroidRuntime(21166):    at com.bitgriff.androidcalls.CallHelper$sendserverhit.doInBackground(CallHelper.java:131)
03-03 12:37:00.999: E/AndroidRuntime(21166):    at com.bitgriff.androidcalls.CallHelper$sendserverhit.doInBackground(CallHelper.java:1)

1 个答案:

答案 0 :(得分:1)

NullPointerException处的错误为java.net.URLEncoder.encode,由于您对字符串和变量numberin进行编码,因此该变量可能为空。