错误.java.lang.NullPointerException

时间:2013-11-27 22:36:09

标签: android

java class:
@Override
        protected String doInBackground(String... params) { 
            InputStream is = null;
            String result = "";

            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("",""));
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(strURL);
            try{
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine())!=null){
                    sb.append(line + "\n");
                }
                is.close();
                result=sb.toString();


                for(int i=0;i<jArray.length();i++){
                    JSONObject json_data = jArray.getJSONObject(i);
                    String pid=""+json_data.getString("pid");
                    String region = json_data.getString("region");
                    String name = json_data.getString("name");
                    Double longi = json_data.getDouble("longitude");
                    Double lati = json_data.getDouble("latitude");
                    //convert double variable to int variable
                    int d1 = (int) (lati * 1E6);
                    int d2 = (int) (longi * 1E6);
                    //convert int variable to String variable:
                    String latitude = ""+ d1;
                    String longitutde = ""+ d2;
                    Toast.makeText(HomePage.this, latitude, Toast.LENGTH_LONG).show();
                }
            } catch(Exception e)
            {
                Log.e("log_tag", "Error in http connection " + e.toString());   
            }
            try{
                jArray = new JSONArray(result);

            }
            catch(JSONException e){
                Log.e("log_tag", "Error parsing data " + e.toString());
            }
            return null;

        }

        @Override
        protected void onPostExecute(String result) {
            pDialog.dismiss();
        }
    }

}

log cat:

11-27 22:57:22.581: E/Trace(1675): error opening trace file: No such file or directory (2)
11-27 22:57:22.881: D/libEGL(1675): loaded /system/lib/egl/libEGL_emulation.so
11-27 22:57:22.885: D/(1675): HostConnection::get() New Host Connection established 0xb9087948, tid 1675
11-27 22:57:22.901: D/libEGL(1675): loaded /system/lib/egl/libGLESv1_CM_emulation.so
11-27 22:57:22.901: D/libEGL(1675): loaded /system/lib/egl/libGLESv2_emulation.so
11-27 22:57:23.185: W/EGL_emulation(1675): eglSurfaceAttrib not implemented
11-27 22:57:23.201: D/OpenGLRenderer(1675): Enabling debug mode 0
11-27 22:57:27.037: W/EGL_emulation(1675): eglSurfaceAttrib not implemented
11-27 22:57:29.181: W/EGL_emulation(1675): eglSurfaceAttrib not implemented
11-27 22:57:29.289: D/dalvikvm(1675): GC_CONCURRENT freed 120K, 3% free 6232K/6400K, paused 6ms+1ms, total 75ms
11-27 22:57:30.549: E/log_tag(1675): Error in http connection java.lang.NullPointerException

1 个答案:

答案 0 :(得分:0)

//你无法在后台运行Toast。 任何UI操作都只能在UI线程中完成。

//从Asynctask

中删除以下行
Toast.makeText(HomePage.this, latitude, Toast.LENGTH_LONG).show();
相关问题