0

谁能告诉我,这里有什么问题?我一直在尝试一整天。我是 android 新手,我也不擅长阅读 logcat,因此,我真的希望有人能帮助我。应用力一直关闭。

private ProgressDialog pDialog;

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

    ArrayList<HashMap<String, String>> eventsList;

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

    // JSON Node names
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_CUSTOMER = "customer";
    private static final String TAG_PID = "pid";
    private static final String TAG_FIRSTNAME = "firstname";
    private static final String TAG_LASTNAME = "lastname";
    private static final String TAG_ADDRESS = "address";
    private static final String TAG_TOTALCOST = "totalCost";

    // products JSONArray
    JSONArray events = null;

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

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

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

    }


    /**
     * Background Async Task to Load all product by making HTTP Request
     * */
    class LoadAllProducts extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(CustomerTransaction.this);
            pDialog.setMessage("Loading. 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 reponse
            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
                    events = json.getJSONArray(TAG_CUSTOMER);

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

                        // Storing each json item in variable
                        String pid = c.getString(TAG_PID);
                        String firstname = c.getString(TAG_FIRSTNAME);
                        String lastname = "Time :" +c.getString(TAG_LASTNAME);
                        String address = "Date :" +c.getString(TAG_ADDRESS);
                        String totalCost = "Venue :" +c.getString(TAG_TOTALCOST);


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

                        // adding each child node to HashMap key => value
                        map.put(TAG_PID, pid);
                        map.put(TAG_FIRSTNAME, firstname);
                        map.put(TAG_LASTNAME, lastname);
                        map.put(TAG_ADDRESS, address);
                        map.put(TAG_TOTALCOST, totalCost);
                        // adding HashList to ArrayList
                        eventsList.add(map);
                    }
                } 

            } 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(
                            CustomerTransaction.this, eventsList,
                            R.layout.list_item4, new String[] { TAG_PID,TAG_FIRSTNAME, TAG_LASTNAME, TAG_ADDRESS, TAG_TOTALCOST},
                            new int[] { R.id.pid, R.id.firstname, R.id.lastname, R.id.address, R.id.totalCost });
                    // updating listview
                    setListAdapter(adapter);
                }
            });

        }

    }

日志猫:

04-16 18:34:22.706: E/WindowManager(1019): Activity com.spyraa.bookstore.CustomerTransaction has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40d2c1e8 V.E..... R.....ID 0,0-304,96} that was originally added here
 04-16 18:34:22.706: E/WindowManager(1019): android.view.WindowLeaked: Activity com.spyraa.bookstore.CustomerTransaction has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40d2c1e8 V.E..... R.....ID 0,0-304,96} that was originally added here
04-16 18:34:22.706: E/WindowManager(1019):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:354)
04-16 18:34:22.706: E/WindowManager(1019):  at  android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:216)
04-16 18:34:22.706: E/WindowManager(1019):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
04-16 18:34:22.706: E/WindowManager(1019):  at android.app.Dialog.show(Dialog.java:281)
 04-16 18:34:22.706: E/WindowManager(1019):     at com.spyraa.bookstore.CustomerTransaction$LoadAllProducts.onPreExecute(CustomerTransaction.java:84)
04-16 18:34:22.706: E/WindowManager(1019):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
 04-16 18:34:22.706: E/WindowManager(1019):     at android.os.AsyncTask.execute(AsyncTask.java:534)
04-16 18:34:22.706: E/WindowManager(1019):  at com.spyraa.bookstore.CustomerTransaction.onCreate(CustomerTransaction.java:61)
04-16 18:34:22.706: E/WindowManager(1019):  at android.app.Activity.performCreate(Activity.java:5104)
04-16 18:34:22.706: E/WindowManager(1019):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-16 18:34:22.706: E/WindowManager(1019):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-16 18:34:22.706: E/WindowManager(1019):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-16 18:34:22.706: E/WindowManager(1019):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-16 18:34:22.706: E/WindowManager(1019):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-16 18:34:22.706: E/WindowManager(1019):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 18:34:22.706: E/WindowManager(1019):  at android.os.Looper.loop(Looper.java:137)
04-16 18:34:22.706: E/WindowManager(1019):  at android.app.ActivityThread.main(ActivityThread.java:5039)
04-16 18:34:22.706: E/WindowManager(1019):  at java.lang.reflect.Method.invokeNative(Native Method)
04-16 18:34:22.706: E/WindowManager(1019):  at java.lang.reflect.Method.invoke(Method.java:511)
04-16 18:34:22.706: E/WindowManager(1019):  at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-16 18:34:22.706: E/WindowManager(1019):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-16 18:34:22.706: E/WindowManager(1019):  at dalvik.system.NativeStart.main(Native Method)

太感谢了。

4

2 回答 2

0

像这样使用请求

HttpPost httpost = new HttpPost(url_all_products);

List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("test1","test1" ));
nvps.add(new BasicNameValuePair("test2", "test2" ));

        httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

response = getResponse(httpost);
于 2013-04-16T19:17:39.653 回答
0

我看到几个问题。第一个我认为是造成你的问题。将您的进度条初始化移出onPreExecute()到类声明中

 class LoadAllProducts extends AsyncTask<String, String, String> {

  pDialog = new ProgressDialog(CustomerTransaction.this);  // move to here so you can cancel in onPostExecute()
    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();

您不需要runOnUiThreadin,onPostExecute()因为此方法在UI线程上运行。您正在接受一个参数,onPostExecute()但似乎没有从doInBackground(). 可能还有更多问题,但我会先尝试这些事情,看看你会得到什么

知道如何使用 logcat 非常重要。在这里它告诉您一个窗口已经泄漏,我认为这是由您初始化progressbar. 通常你会看到一行写着“由空指针异常引起”或其他异常。当您找到此行时,引用您的项目的第一行

 at com.spyraa.bookstore.CustomerTransaction$LoadAllProducts.onPreExecute(CustomerTransaction.java:84)

例如,将告诉您第 84 行是您的错误发生的位置。这通常是开始寻找的好地方。希望这可以帮助

于 2013-04-16T19:20:25.590 回答