1

我想使用 PHP 从 MySQL 数据库中获取数据并将其显示在 Android 活动中。我对其进行编码并传递 JSON 数组,但有一个问题我不知道如何连接到服务器,并且我的所有数据库都在本地服务器上。我对其进行编码 请告诉我哪里出错了,以便我得到准确的结果。我会非常感谢你。

我的所有数据库都在本地主机中,并且没有打开活动。显示错误“意外停止”:(我怎样才能得到准确的结果。请指导我

现在它给出了这些错误:(并加载保存的详细信息

    12-08 11:09:19.314: E/WindowManager(19972): Activity com.example.visit.record.My_Task has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40555850 that was originally added here
12-08 11:09:19.314: E/WindowManager(19972): android.view.WindowLeaked: Activity com.example.visit.record.My_Task has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40555850 that was originally added here
12-08 11:09:19.314: E/WindowManager(19972):     at android.view.ViewRoot.<init>(ViewRoot.java:277)
12-08 11:09:19.314: E/WindowManager(19972):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
12-08 11:09:19.314: E/WindowManager(19972):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
12-08 11:09:19.314: E/WindowManager(19972):     at android.view.Window$LocalWindowManager.addView(Window.java:433)
12-08 11:09:19.314: E/WindowManager(19972):     at android.app.Dialog.show(Dialog.java:288)
12-08 11:09:19.314: E/WindowManager(19972):     at com.example.visit.record.My_Task$GetTaskDetails.onPreExecute(My_Task.java:149)
12-08 11:09:19.314: E/WindowManager(19972):     at android.os.AsyncTask.execute(AsyncTask.java:391)
12-08 11:09:19.314: E/WindowManager(19972):     at com.example.visit.record.My_Task.onCreate(My_Task.java:107)
12-08 11:09:19.314: E/WindowManager(19972):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
12-08 11:09:19.314: E/WindowManager(19972):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1785)
12-08 11:09:19.314: E/WindowManager(19972):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1842)
12-08 11:09:19.314: E/WindowManager(19972):     at android.app.ActivityThread.access$1500(ActivityThread.java:132)
12-08 11:09:19.314: E/WindowManager(19972):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
12-08 11:09:19.314: E/WindowManager(19972):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-08 11:09:19.314: E/WindowManager(19972):     at android.os.Looper.loop(Looper.java:143)
12-08 11:09:19.314: E/WindowManager(19972):     at android.app.ActivityThread.main(ActivityThread.java:4268)
12-08 11:09:19.314: E/WindowManager(19972):     at java.lang.reflect.Method.invokeNative(Native Method)
12-08 11:09:19.314: E/WindowManager(19972):     at java.lang.reflect.Method.invoke(Method.java:507)
12-08 11:09:19.314: E/WindowManager(19972):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-08 11:09:19.314: E/WindowManager(19972):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-08 11:09:19.314: E/WindowManager(19972):     at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

0

目前您正在尝试访问 Ui 元素或runOnUiThread 在classdoInBackground中。GetProductDetails AsyncTask因为无法从doInBackground您可以使用 onPreExecuteonPostExecute更新 UI 元素中访问 UI 元素。将代码更改为不使用runOnUiThread或内部的 UI 元素doInBackground

      class GetProductDetails 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.setMessage("Loading task details. Please wait...");
            tDialog.setIndeterminate(false);
            tDialog.setCancelable(true);
            tDialog.show();
        }
   protected JSONObject doInBackground(String... params) {

                   JSONObject my_task 
                    // 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;
        }
        protected void onPostExecute(JSONObject my_task) {
            // dismiss the dialog once got all details

            // task with this cid found
            // Edit Text
             // display task data in EditText

             cus_name_txtbx = (EditText) findViewById(R.id.cus_name_txt);
             cus_name_txtbx.setText(my_task.getString(TAG_NAME));

             contact_no_txtbx = (EditText) findViewById(R.id.contact_no_txt);
             contact_no_txtbx.setText(my_task.getString(TAG_CONTACT));

             ticket_no_txtbx = (EditText) findViewById(R.id.ticket_no_txt);
             ticket_no_txtbx.setText(my_task.getString(TAG_TICKET));

             task_detail_txtbx = (EditText) findViewById(R.id.task_detail_txt);
             task_detail_txtbx.setText(my_task.getString(TAG_TASKDETAIL));
            tDialog.dismiss();
        }

并将您的 onCreate 代码更改为:

public class My_Task extends Activity {

        TextView cus_name_txt, contact_no_txt, ticket_no_txt, task_detail_txt,attend_by_txts;
        EditText attend_by_txtbx, cus_name_txtbx, contact_no_txtbx, ticket_no_txtbx, task_detail_txtbx;
        Button btnSave;
        Button btnDelete;

////YOUR CODE here

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


        cus_name_txt = (TextView) findViewById(R.id.cus_name_txt);

        contact_no_txt = (TextView)findViewById(R.id.contact_no_txt);

        ticket_no_txt = (TextView)findViewById(R.id.ticket_no_txt);

        task_detail_txt = (TextView)findViewById(R.id.task_detail_txt);

        attend_by_txts = (TextView)findViewById(R.id.attend_by_txt;

        attend_by_txtbx = (EditText)findViewById(R.id.attend_by_txtbx);
        attend_by_txtbx.setText(attend_by_txt);
        //your code here
于 2012-12-08T04:40:08.313 回答
0

一件事是显而易见的。您不能UI从后台线程更改。

在这里你在做什么

cus_name_txtbx = (EditText) findViewById(R.id.cus_name_txt);
                        cus_name_txtbx.setText(my_task.getString(TAG_NAME));

                        contact_no_txtbx = (EditText) findViewById(R.id.contact_no_txt);
                        contact_no_txtbx.setText(my_task.getString(TAG_CONTACT));

                        ticket_no_txtbx = (EditText) findViewById(R.id.ticket_no_txt);
                        ticket_no_txtbx.setText(my_task.getString(TAG_TICKET));

                        task_detail_txtbx = (EditText) findViewById(R.id.task_detail_txt);
                        task_detail_txtbx.setText(my_task.getString(TAG_TASKDETAIL));

doInBackgroundGetProductDetails

于 2012-12-08T04:41:31.860 回答