E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2

Posted

技术标签:

【中文标题】E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2【英文标题】:E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #2 【发布时间】:2016-07-14 20:54:12 【问题描述】:

我正在为此苦苦挣扎..请帮助我。我也附上了 logcat 输出。请帮我解决这个问题。我从 localhost url 调用数据库,因为我将数据库保存在 XAMPP mysql 中。问题是 ListView 根本没有显示任何内容。应用程序正在崩溃。

public class Delivery_info extends ListActivity 

private ProgressDialog pDialog;

// Creating JSON Parser object
JSONParser jParser = new JSONParser();


ArrayList<HashMap<String, String>> productsList;

// url to get all products list
private static String url_all_products = "http:/127.0.0.1/hul/get_all_products.php";

// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "product";
private static final String TAG_DATE = "created_on";
private static final String TAG_DELIVERY = "delivery_id";
private static final String TAG_INVOICE = "invoice_amt";


// products JSONArray
JSONArray products = null;

@Override
public void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_delivery_info);

    // Hashmap for ListView
    productsList = new ArrayList<HashMap<String, String>>();

    // Loading products in Background Thread
    new LoadAllProducts().execute();

    // Get listview
    ListView lv = getListView();

    // on seleting single product
    // launching Edit Product Screen
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() 

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) 
            // getting values from selected ListItem
            String pid = ((TextView) view.findViewById(R.id.pid)).getText()
                    .toString();

            // Starting new intent
            /**Intent in = new Intent(getApplicationContext(), Finances.class);
            // sending pid to next activity
            in.putExtra(TAG_PRODUCTS, pid);

            // starting new activity and expecting some response back
            startActivityForResult(in, 100);**/
        
    );




// Response from Edit Product Activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    super.onActivityResult(requestCode, resultCode, data);
    // if result code 100
    if (resultCode == 100) 
        // if result code 100 is received
        // means user edited/deleted product
        // reload this screen again
        Intent intent = getIntent();
        finish();
        startActivity(intent);
    


class LoadAllProducts extends AsyncTask<String, String, String> 

    /**
     * Before starting background thread Show Progress Dialog
     */
    @Override
    protected void onPreExecute() 
        super.onPreExecute();
        pDialog = new ProgressDialog(Delivery_info.this);
        pDialog.setMessage("Loading products. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    

    /**
     * getting All products from url
     */
    protected String doInBackground(String... args) 
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        // getting JSON string from URL
        JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
        // Check your log cat for JSON response
        Log.d("All Products: ", json.toString());

        try 
            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) 
                // products found
                // Getting Array of Products
                products = json.getJSONArray(TAG_PRODUCTS);

                // looping through All Products
                for (int i = 0; i < products.length(); i++) 
                    JSONObject c = products.getJSONObject(i);

                    // Storing each json item in variable
                    String id = c.getString(TAG_PRODUCTS);
                    String name = c.getString(TAG_DATE);

                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_PRODUCTS, id);
                    map.put(TAG_DATE, name);

                    // adding HashList to ArrayList
                    productsList.add(map);
                
            /** else 
                // no products found
                // Launch Add New product Activity
                Intent i = new Intent(getApplicationContext(), NewProductActivity.class);
                // Closing all previous activities
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);
            **/
         catch (JSONException e) 
            e.printStackTrace();
        

        return null;
    

    /**
     * After completing background task Dismiss the progress dialog
     * *
     */
    protected void onPostExecute(String file_url) 
        // dismiss the dialog after getting all products
        pDialog.dismiss();
        // updating UI from Background Thread
        runOnUiThread(new Runnable() 
            public void run() 
                /**
                 * Updating parsed JSON data into ListView
                 * */
                ListAdapter adapter = new SimpleAdapter(
                        Delivery_info.this, productsList,
                        R.layout.list_item, new String[]TAG_PRODUCTS,
                        TAG_DATE,
                        new int[]R.id.pid, R.id.name);
                // updating listview
                setListAdapter(adapter);
            
        );

    


    03-27 18:55:53.805  19554-19750/com.example.hul.hul E/androidRuntime﹕ FATAL EXCEPTION: AsyncTask #2
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:299)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
            at java.util.concurrent.FutureTask.run(FutureTask.java:239)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
            at java.lang.Thread.run(Thread.java:841)
     Caused by: java.lang.IllegalArgumentException: Host name may not be null
            at org.apache.http.HttpHost.<init>(HttpHost.java:83)
            at org.apache.http.impl.client.AbstractHttpClient.determineTarget(AbstractHttpClient.java:497)
            at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
            at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
            at com.example.hul.hul.JSONParser.makeHttpRequest(JSONParser.java:63)
            at com.example.hul.hul.Delivery_info$LoadAllProducts.doInBackground(Delivery_info.java:129)
            at com.example.hul.hul.Delivery_info$LoadAllProducts.doInBackground(Delivery_info.java:107)
            at android.os.AsyncTask$2.call(AsyncTask.java:287)
            at java.util.concurrent.FutureTask.run(FutureTask.java:234)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
            at java.lang.Thread.run(Thread.java:841)
03-27 18:55:54.065  19554-19554/com.example.hul.hul E/WindowManager﹕ Activity com.example.hul.hul.Delivery_info has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView4204c9d0 V.E..... R......D 0,0-684,192 that was originally added here
    android.view.WindowLeaked: Activity com.example.hul.hul.Delivery_info has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView4204c9d0 V.E..... R......D 0,0-684,192 that was originally added here
            at android.view.ViewRootImpl.<init>(ViewRootImpl.java:350)
            at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:239)
            at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
            at android.app.Dialog.show(Dialog.java:289)
            at com.example.hul.hul.Delivery_info$LoadAllProducts.onPreExecute(Delivery_info.java:119)
            at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
            at android.os.AsyncTask.execute(AsyncTask.java:534)
            at com.example.hul.hul.Delivery_info.onCreate(Delivery_info.java:59)
            at android.app.Activity.performCreate(Activity.java:5133)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)
            at android.app.ActivityThread.access$600(ActivityThread.java:150)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:213)
            at android.app.ActivityThread.main(ActivityThread.java:5225)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
            at dalvik.system.NativeStart.main(Native Method)
03-27 19:07:07.163  21510-21529/com.example.hul.hul E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:299)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
            at java.util.concurrent.FutureTask.run(FutureTask.java:239)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
            at java.lang.Thread.run(Thread.java:841)
     Caused by: java.lang.IllegalArgumentException: Host name may not be null
            at org.apache.http.HttpHost.<init>(HttpHost.java:83)
            at org.apache.http.impl.client.AbstractHttpClient.determineTarget(AbstractHttpClient.java:497)
            at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
            at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
            at com.example.hul.hul.JSONParser.makeHttpRequest(JSONParser.java:63)
            at com.example.hul.hul.Delivery_info$LoadAllProducts.doInBackground(Delivery_info.java:129)
            at com.example.hul.hul.Delivery_info$LoadAllProducts.doInBackground(Delivery_info.java:107)
            at android.os.AsyncTask$2.call(AsyncTask.java:287)
            at java.util.concurrent.FutureTask.run(FutureTask.java:234)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
            at java.lang.Thread.run(Thread.java:841)
03-27 19:07:07.534  21510-21510/com.example.hul.hul E/WindowManager﹕ Activity com.example.hul.hul.Delivery_info has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView4204d2c0 V.E..... R......D 0,0-684,192 that was originally added here
    android.view.WindowLeaked: Activity com.example.hul.hul.Delivery_info has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView4204d2c0 V.E..... R......D 0,0-684,192 that was originally added here
            at android.view.ViewRootImpl.<init>(ViewRootImpl.java:350)
            at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:239)
            at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
            at android.app.Dialog.show(Dialog.java:289)
            at com.example.hul.hul.Delivery_info$LoadAllProducts.onPreExecute(Delivery_info.java:119)
            at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
            at android.os.AsyncTask.execute(AsyncTask.java:534)
            at com.example.hul.hul.Delivery_info.onCreate(Delivery_info.java:59)
            at android.app.Activity.performCreate(Activity.java:5133)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)
            at android.app.ActivityThread.access$600(ActivityThread.java:150)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:213)
            at android.app.ActivityThread.main(ActivityThread.java:5225)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
            at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

你能从你的浏览器/邮递员中点击 URL 并检查它是否在那里给出正确的响应吗? 是的..它给出了正确的响应/... 【参考方案1】:

这里有 2 个问题:

1- 127.0.0.1 不应该在移动设备上使用,因为它指的是服务器 这不是手机本身。所以将该IP更改为服务器或测试机器的实际IP地址(安装xampp)

2- URL 本身有错误(错字):http:

后缺少 /
private static String url_all_products = "http:/127.0.0.1/hul/get_all_products.php";

应该是

private static String url_all_products = "http://127.0.0.1/hul/get_all_products.php";

所以您的最终网址应如下所示(示例):

private static String url_all_products = "http://192.168.1.10/hul/get_all_products.php";

甚至是域名:

private static String url_all_products = "http://my.api.domain/hul/get_all_products.php";

【讨论】:

【参考方案2】:

如果您的服务响应是在外部(PostMan)中完成的,那么您必须签入 Java 代码,这意味着清单中的 Internet 权限。

【讨论】:

以上是关于E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2的主要内容,如果未能解决你的问题,请参考以下文章

Android Studio 标准列表视图:E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1

java.lang.NoSuchMethodError E / AndroidRuntime:FATAL EXCEPTION:Thread-4进程:com.hikvision.open.app,PID

Binary XML file line #2: Error inflating

E/AndroidRuntime(3306): java.lang.RuntimeException: 无法启动活动 ComponentInfoc [重复]

Proguard Retrace 不适用于 E/AndroidRuntime(10237) 等堆栈跟踪运行时信息:

E/AndroidRuntime:致命异常:Android 的 AsyncTask #1 错误