2016-05-17 166 views
1

我在android設備上構建我的代碼時出錯。 其錯誤報告致命異常:AsyncTask#1,java.lang.RuntimeException:執行doInBackground()時發生錯誤

9月5日至17日:47:16.035 17982-18134/com.example.ckw.mapku E/AndroidRuntime:致命異常:#的AsyncTask 1 9月5日至17日:47:16.035 17982 -18134/com.example.ckw.mapku E/AndroidRuntime:進程:com.example.ckw.mapku,PID:17982 05-17 09:47:16.035 17982-18134/com.example.ckw.mapku E/AndroidRuntime :java.lang.RuntimeException:執行doInBackground()時發生錯誤 05-17 09:47:16.035 17982-18134/com.example.ckw.mapku E/AndroidRuntime:at android.os.AsyncTask $ 3.done(AsyncTask .java:300) 05-17 09:47:16.035 17982-18134/com.example.ckw.mapku E/AndroidRuntime:在java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) 05-17 09:47:16.035 17982-18134/com.example.ckw.mapku E/AndroidRuntime:at java.util.concurrent.FutureTask.setException(FutureTask.java:222) 05-17 09:47:16.035 17982-18134/com.example.ckw.mapku E/AndroidRuntime:在java.util.concurrent.FutureTask.run(FutureTask.java:242) 05-17 09:47:16.035 17982-18134/com.example.ckw .mapku E/AndroidRuntime:at android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231) 05-17 09:47:16.035 17982-18134/com.example.ckw.mapku E/AndroidRuntime:at java .util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 05-17 09:47:16.035 17982-18134/com.example.ckw.mapku E/AndroidRuntime:在java.util.concurrent.ThreadPoolExecutor $ Worker。運行(ThreadPoolExecutor.java:587) 05-17 09:47:16.035 17982-18134/com.example.ckw.mapku E/AndroidRuntime:at java.lang.Thread.run( Thread.java:841) 05-17 09:47:16.035 17982-18134/com.example.ckw.mapku E/AndroidRuntime:導致:java.lang.NullPointerException 05-17 09:47:16.035 17982-18134 /com.example.ckw.mapku E/AndroidRuntime:at com.example.ckw.mapku.ServiceHandler.makeHttpRequest(ServiceHandler.java:71) 05-17 09:47:16.035 17982-18134/com.example.ckw。 mapku E/AndroidRuntime:at com.example.ckw.mapku.ServiceHandler.makeHttpRequest(ServiceHandler.java:33) 05-17 09:47:16.035 17982-18134/com.example.ckw.mapku E/AndroidRuntime:at com .example.ckw.mapku.Hasil_pencarian $ GetMedik.doInBackground(Hasil_pencarian.java:77) 05-17 09:47:16.035 17982-18134/com.example.ckw.mapku E/AndroidRuntime:at com.example.ckw。 mapku.Hasil_pencarian $ GetMedik.doInBackground(Hasil_pencarian.java:60) 05-17 09:47:16.035 17982-18134/com.example.ckw.map ku E/AndroidRuntime:at android.os.AsyncTask $ 2.call(AsyncTask.java:288) 05-17 09:47:16.035 17982-18134/com.example.ckw.mapku E/AndroidRuntime:在java.util。併發.FutureTask.run(FutureTask.java:237) 05-17 09:47:16.035 17982-18134/com.example.ckw.mapku E/AndroidRuntime:在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask。 java:231) 05-17 09:47:16.035 17982-18134/com.example.ckw.mapku E/AndroidRuntime:在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 05-17 09 :47:16.035 17982-18134/com.example.ckw.mapku E/AndroidRuntime:在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:587) 05-17 09:47:16.035 17982-18134 /com.example.ckw.mapku E/AndroidRuntime:在java.lang.Thread.run(Thread。Java的:841)

,這是我的代碼

package com.example.ckw.mapku; 

import android.app.ProgressDialog; 
import android.app.Service; 
import android.os.AsyncTask; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
import android.widget.TextView; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpRequest; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.params.BasicHttpParams; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 
import org.json.JSONStringer; 

import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 
import java.util.HashMap; 
public class Hasil_pencarian extends AppCompatActivity { 

    String myJSON; 

    private static final String TAG_RESULT = "result"; 
    private static final String TAG_ID = "id_lay"; 
    private static final String TAG_NAME = "Nama"; 
    private static final String TAG_LONG = "Longitude"; 
    private static final String TAG_LAT = "Latitude"; 

    JSONArray layanan = null; 

    ArrayList<HashMap<String, String>> layananList; 

    ListView list; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_hasil_pencarian); 
     list = (ListView) findViewById(R.id.listView); 
     layananList = new ArrayList<HashMap<String, String>>(); 
     new GetMedik().execute(); 
    } 

    private class GetMedik extends AsyncTask<String, Void, JSONObject>{ 
     private ProgressDialog progressDialog; 

     private static final String ALAMAT = "http://rugimuleh.com/medikmap/medik.php"; 

     @Override 
     protected void onPreExecute(){ 
      progressDialog = new ProgressDialog(Hasil_pencarian.this); 
      progressDialog.setMessage("Tunggu sebentar..."); 
      progressDialog.setCancelable(false); 
      progressDialog.show(); 

     } 

     @Override 
     protected JSONObject doInBackground(String... arg0) { 
      ServiceHandler sh = new ServiceHandler(); 
      JSONObject jsonStr = sh.makeHttpRequest(ALAMAT, "GET"); 
      Log.d("JSON result ", jsonStr.toString()); 
      myJSON = jsonStr.toString(); 

      if (jsonStr != null) { 
       try { 
        JSONObject jsonObject = new JSONObject(myJSON); 
        layanan = jsonObject.getJSONArray(TAG_RESULT); 

        for (int i=0;i<layanan.length();i++){ 
         JSONObject c = layanan.getJSONObject(i); 
         String id_lay = c.getString(TAG_ID); 
         String Nama = c.getString(TAG_NAME); 
         String Longitude = c.getString(TAG_LONG); 
         String Latitude = c.getString(TAG_LAT); 

         HashMap<String, String> medik = new HashMap<String, String>(); 

         medik.put(TAG_ID, id_lay); 
         medik.put(TAG_NAME, Nama); 
         medik.put(TAG_LONG, Longitude); 
         medik.put(TAG_LAT, Latitude); 

         layananList.add(medik); 
        } 
       }catch (Exception e) { 
        e.printStackTrace(); 
       } 
      }else { 
       Log.e("ServiceHandler", "Couldn't get any data from the url");} 
      return null; 
     } 

     protected void onPostExecute(JSONObject result){ 
      super.onPostExecute(result); 

      if(progressDialog.isShowing()) 
       progressDialog.dismiss(); 

      ListAdapter adapter = new SimpleAdapter(
        Hasil_pencarian.this, layananList, R.layout.list_item, 
        new String[]{TAG_ID, TAG_NAME, TAG_LONG, TAG_LAT}, 
        new int[]{R.id.id_lay, R.id.Nama, R.id.Longitude, R.id.Latitude} 
      ); 
      list.setAdapter(adapter); 

     } 
    } 

}

+1

有來自拋出顯示java.lang.NullPointerException ServiceHandler.java的第71行。您應該檢查該文件。 –

+0

好吧,我明白了。這非常有幫助謝謝 –

回答

1

檢查一個NullPointerException異常這裏:

 JSONObject jsonStr = sh.makeHttpRequest(ALAMAT, "GET"); 

引起:java.lang中.NullPointerException 05-17 09:47:16.035 17982-18134/com.example.ckw.mapku E/AndroidRuntime:在 com.example.ckw.mapku.ServiceHandler.makeHttpRequest(ServiceHandler.java:71)

+0

好吧,我明白了。問題出在我的聲明中。謝謝。但我又有一個問題。當我的應用程序運行時沒有錯誤,但我的php不能顯示在我的應用程序中的數據。如何回合 –

+0

@StepHanusCkw,可以繼續作爲另一個問題,這應該是足夠的本身:) – nullpointer

+0

好ty爲您的幫助@nullpointer –

相關問題