0

我被困住并且发疯了来解决这个问题,但我没有运气。我尝试了 SO 成员给我的所有指导方针,正如我之前问过这个问题一样我可以解决这个 JSONParsing 吗?我从 PHP 发布数据并希望在 android EditText 框中获取它。我对其进行了编码,但是从第一天开始就存在一些问题,但尚未解决。我在模拟器和电话上遇到了不同的 LogCat 错误. 我已经向你展示了我的 java 代码和 LogCat 错误。任何指南对我来说都是宝贵的。

我的 Java 代码是我出错的地方:

// save button
    btnSave = (Button) findViewById(R.id.btnSave);

    // getting product details from intent
    Intent i = getIntent();

    // getting product id (pid) from intent
    cid = i.getStringExtra(TAG_CID);

    // save button click event
    btnSave.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // starting background task to update product
            new SaveTaskDetails().execute();
        }
    });

    }

@Override
protected void onStart() {
  super.onStart();
   // Getting complete product details in background thread
  new GetTaskDetails().execute();
}

/**
 * Background Async Task to Get complete product details
 * */
class GetTaskDetails extends AsyncTask<String, String, JSONObject> {

    /**
     * Before starting background thread Show Progress Dialog
     * */


    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        tDialog = new ProgressDialog(My_Task.this);
        tDialog.setTitle("Please Wait");
        tDialog.setMessage("Retrieving Task Details.");
        tDialog.setIndeterminate(false);
        tDialog.setCancelable(true);
        tDialog.show();

    }

    /**
     * Getting task details in background thread
     * @return 
     **/

   protected JSONObject doInBackground(String... params1) {

      JSONObject my_task = null;
           // updating UI from Background Thread
                  // Check for success tag
                int success;
                try {
                    // Building Parameters
                    List<NameValuePair> params = new ArrayList<NameValuePair>();
                    params.add(new BasicNameValuePair("cid", cid));

                    // getting product details by making HTTP request
                    // Note that product details url will use GET request
                    JSONObject json = JSONParser.makeHttpRequest(
                            url_read_mytask, "GET", params);

                    // check your log for json response
     //                Log.d("Single Task Details", json.toString());

                    // json success tag
                    success = json.getInt(TAG_SUCCESS);
                    if (success == 1) {
                        // successfully received product details
                        JSONArray my_taskObj = json
                                .getJSONArray(TAG_MYTASK); // JSON Array

                        // get first product object from JSON Array
                       my_task = my_taskObj.getJSONObject(0);

                         }
                    else
                    {
                        // task with cid not found

                    }
                } 
                catch (JSONException e) {
                    e.printStackTrace();
                }


        return my_task;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/


    protected void onPostExecute(JSONObject my_task) {
        // dismiss the dialog once got all details
        cus_name_txtbx = (EditText) findViewById(R.id.cus_name_txtbx);
        try {
            cus_name_txtbx.setText(my_task.getString(TAG_NAME));
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        contact_no_txtbx = (EditText) findViewById(R.id.contact_no_txtbx);
        try {
            contact_no_txtbx.setText(my_task.getString(TAG_CONTACT));
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        ticket_no_txtbx = (EditText) findViewById(R.id.ticket_no_txtbx);
        try {
            ticket_no_txtbx.setText(my_task.getString(TAG_TICKET));
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        task_detail_txtbx = (EditText) findViewById(R.id.task_detail_txtbx);
        try {
            task_detail_txtbx.setText(my_task.getString(TAG_TASKDETAIL));
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }           

        tDialog.dismiss(); 
    }
}

/**
 * Background Async Task to  Save product Details
 * */
class SaveTaskDetails extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        tDialog = new ProgressDialog(My_Task.this);
        tDialog.setTitle("Please Wait");
        tDialog.setMessage("Saving Task");
        tDialog.setIndeterminate(false);
        tDialog.setCancelable(true);
        tDialog.show();
    }

    /**
     * Saving product
     * */
    protected String doInBackground(String... args) {

        // getting updated data from EditTexts
        String cus_name = cus_name_txtbx.getText().toString();
        String contact_no = contact_no_txtbx.getText().toString();
        String ticket_no = ticket_no_txtbx.getText().toString();
        String task_detail = task_detail_txtbx.getText().toString();

        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair(TAG_CID, cid));
        params.add(new BasicNameValuePair(TAG_NAME, cus_name));
        params.add(new BasicNameValuePair(TAG_CONTACT, contact_no));
        params.add(new BasicNameValuePair(TAG_TICKET, ticket_no));
        params.add(new BasicNameValuePair(TAG_TASKDETAIL, task_detail));

        // sending modified data through http request
        // Notice that update product url accepts POST method
        JSONObject json = JSONParser.makeHttpRequest(url_update_mytask,
                "POST", params);

        // check json success tag
        try {
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // successfully updated
                Intent i = getIntent();
                // send result code 100 to notify about product update
                setResult(100, i);
                finish();
            } else {
                // failed to update product
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog once product uupdated
        tDialog.dismiss();
    }
}

我的 Logcat 错误是:

    01-01 11:28:52.549: E/WindowManager(31782):     at java.lang.reflect.Method.invokeNative(Native Method)
01-01 11:38:15.149: E/WindowManager(1133): Activity com.example.visit.record.My_Task has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4054ce88 that was originally added here
01-01 11:38:15.149: E/WindowManager(1133): android.view.WindowLeaked: Activity com.example.visit.record.My_Task has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4054ce88 that was originally added here
01-01 11:38:15.149: E/WindowManager(1133):  at android.view.ViewRoot.<init>(ViewRoot.java:277)
01-01 11:38:15.149: E/WindowManager(1133):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
01-01 11:38:15.149: E/WindowManager(1133):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
01-01 11:38:15.149: E/WindowManager(1133):  at android.view.Window$LocalWindowManager.addView(Window.java:433)
01-01 11:38:15.149: E/WindowManager(1133):  at android.app.Dialog.show(Dialog.java:288)
01-01 11:38:15.149: E/WindowManager(1133):  at com.example.visit.record.My_Task$GetTaskDetails.onPreExecute(My_Task.java:139)
01-01 11:38:15.149: E/WindowManager(1133):  at android.os.AsyncTask.execute(AsyncTask.java:391)
01-01 11:38:15.149: E/WindowManager(1133):  at com.example.visit.record.My_Task.onStart(My_Task.java:118)
01-01 11:38:15.149: E/WindowManager(1133):  at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1225)
01-01 11:38:15.149: E/WindowManager(1133):  at android.app.Activity.performStart(Activity.java:3941)
01-01 11:38:15.149: E/WindowManager(1133):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1794)
01-01 11:38:15.149: E/WindowManager(1133):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1842)
01-01 11:38:15.149: E/WindowManager(1133):  at android.app.ActivityThread.access$1500(ActivityThread.java:132)
01-01 11:38:15.149: E/WindowManager(1133):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
01-01 11:38:15.149: E/WindowManager(1133):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-01 11:38:15.149: E/WindowManager(1133):  at android.os.Looper.loop(Looper.java:143)
01-01 11:38:15.149: E/WindowManager(1133):  at android.app.ActivityThread.main(ActivityThread.java:4268)
01-01 11:38:15.149: E/WindowManager(1133):  at java.lang.reflect.Method.invokeNative(Native Method)
01-01 11:38:15.149: E/WindowManager(1133):  at java.lang.reflect.Method.invoke(Method.java:507)
01-01 11:38:15.149: E/WindowManager(1133):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-01 11:38:15.149: E/WindowManager(1133):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-01 11:38:15.149: E/WindowManager(1133):  at dalvik.system.NativeStart.main(Native Method)

请指导。我在new GetTaskDetails().execute()上遇到错误 ;并在 **tDialog.show();**GetTaskDetails 类的 PreExecute 上

4

2 回答 2

0

解决方案是在退出 Activity 之前调用dismiss()您在 viewP.java:183 中创建的对话框,例如在 onPause() 中。

视图具有对其父上下文的引用(取自构造函数参数)。如果您离开 Activity 而不销毁Dialogs其他动态创建的视图,它们仍然持有对您的 Activity 的引用(如果您使用 this 作为 Context: 像 new 一样创建ProgressDialog(this)

因此,它无法被 GC 收集,从而导致内存泄漏。

于 2013-01-01T06:04:18.017 回答
0

您在后台运行您的 UI 并从它们中删除,然后在执行后您可以设置 (e.gcus_name_txtbx.setText(my_task.getString(TAG_NAME));) 您没有在 doIn 中设置它们 romove 每个 UI 到 doIn..

于 2013-01-01T06:27:40.597 回答