当前位置: 首页 > 知识库问答 >
问题:

致命异常:AsyncTask#1,java.lang.RuntimeException:执行doInBackground()时出错

西门胜涝
2023-03-14

我在android设备上构建代码时出错了。这是错误报告

05-17 09:47:16.035 17982-18134/com . example . ckw . mapku E/Android runtime:致命异常:async task # 1 05-17 09:47:16.035 17982-18134/com . example . mapku E/Android runtime:Process:com . example . ckw . mapku, PID:17982 05-17 09:47:16.035 17982-18134/com . example . ckw . mapku E/Android runtime:Java . lang . runtime异常:执行doInBackground()05-17 09:47:16.035 17982-18134/com . example . ckw . mapku E/Android runtime:at Android . OS . async task $ 3 . done 05-17 09:47:16.035 17982-18134/com . example . ckw . mapku E/Android runtime:at com . example . ckw . mapku . Hasil _ pen carian $ getmedik . doinbackground(Hasil _ pen carian . Java:77)05-17 09:47:16.035 17982-18134/com . example . ckw . mapku E/Android runtime:at

这是我的密码

    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个答案

高才
2023-03-14

在此处检查NullPointerException:

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

原因: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)

 类似资料:
  • 我做了一个应用程序登录,一切都是正常的昨天,但今天android工作室给了我这个错误 {08-03 14:29:07.894 137 03-14015/Complete.Lyne.MyApplication E/AndroidRuntime:致命异常:AsyncTask#1进程:Complete.Lyne.MyApplication,PID:13703 java.lang.RuntimeExcep

  • 我在android设备上构建代码时出错。这是错误报告 致命异常:AsyncTask#1进程:com。实例冈图,PID:1248爪哇。lang.RuntimeException:在android上执行doInBackground()时出错。操作系统。异步任务3美元。在java上完成(AsyncTask.java:304)。util。同时发生的未来任务。在java完成(FutureTask.java:

  • 早上好StackOver Flow Pros, 我需要一点帮助。我正在运行一个Android应用程序,该应用程序通过php脚本调用MySql数据库,当我运行该应用程序时,我得到一个由空指针异常导致的AsyncTask #1的致命异常错误。它说是在执行doInBackground时发生的。这是我从MyBringBack跟随的一个教程。我真的需要好好想想这件事,因为我已经想了好几天了。如果我删除/注释

  • java.lang.RuntimeException:执行doInBackground()E/Android runtime(695)时出错:at Android . OS . async task $ 3 . done(async task . Java:200)E/Android runtime(695):at Java . util . concurrent . future task $

  • 活动: 类 updateTwitterStatus extends AsyncTask { 这就是 LogCat 06-05 18:00:26.696: E/AndroidRuntime(1516): 致命异常: AsyncTask #2 06-05 18:00:26.696: E/AndroidRuntime(1516): java.lang.RuntimeException: 执行 doInB