2015-04-28 158 views
0

我想从phpMyAdmin使用JSON与PHP获取数据。我正在使用Android Studio。我试图使用进度对话框,然后Windows泄漏(已经使用p.Dialog.dismiss())。之后,我删除了进度对话框,然后发生新的异常。有没有其他解决方案来解决这个问题?有没有其他解决方法,CalledFromWrongThreadException?

这是我的logcat。

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:838) 
    Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 
      at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:5351) 
      at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:970) 
      at android.view.View.requestLayout(View.java:15722) 
      at android.view.View.requestLayout(View.java:15722) 
      at android.view.View.requestLayout(View.java:15722) 
      at android.view.View.requestLayout(View.java:15722) 
      at android.view.View.requestLayout(View.java:15722) 
      at android.view.View.requestLayout(View.java:15722) 
      at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:318) 
      at android.view.View.requestLayout(View.java:15722) 
      at android.widget.TextView.checkForRelayout(TextView.java:6605) 
      at android.widget.TextView.setText(TextView.java:3804) 
      at android.widget.TextView.setText(TextView.java:3662) 
      at android.widget.TextView.setText(TextView.java:3637) 
      at com.example.ayim.madoc.docProfile$DocProfile.doInBackground(docProfile.java:113) 
      at com.example.ayim.madoc.docProfile$DocProfile.doInBackground(docProfile.java:67) 
      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:838) 

这是我写的代码。

package com.example.ayim.madoc; 

import android.app.ProgressDialog; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.TextView; 
import android.widget.Toast; 
import org.apache.http.NameValuePair; 
import org.apache.http.message.BasicNameValuePair; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 
import java.util.ArrayList; 
import java.util.List; 


public class docProfile extends ActionBarActivity { 

    TextView nameDoc; 
    TextView iddoc; 
    TextView icdoc; 
    TextView adddoc; 
    TextView notel; 

    private ProgressDialog pDialog; 

    //JSONArray docprofile = null; 

    JSONParser jsonParser = new JSONParser(); 

    private static final String LOGIN_URL = "http://104.223.3.210/madoc/docprofile.php"; 

    //JSON element ids from repsonse of php script: 
    private static final String TAG_SUCCESS = "success"; 
    private static final String TAG_MESSAGE = "message"; 
    private static final String TAG_DOCTOR = "doctor"; 
    private static final String TAG_ID = "id"; 
    private static final String TAG_NAME = "name"; 
    private static final String TAG_IC = "ic"; 
    private static final String TAG_ADDRESS = "address"; 
    private static final String TAG_NOTEL = "notel"; 
    //String iddoctor; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_doc_profile); 


     // Intent i = getIntent(); 

     //iddoctor = i.getStringExtra("idDoc"); 

     new DocProfile().execute(); 

     //nameDoc = (TextView) findViewById(R.id.docId); 
     //nameDoc.setText(getIntent().getExtras().getString("idDoc")); 


    } 


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


/* 
     @Override 
     protected void onPreExecute() { 

      super.onPreExecute(); 
      pDialog = new ProgressDialog(docProfile.this); 
      pDialog.setMessage("Loading Profile. Please wait...."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(true); 
      pDialog.show(); 
     } 
*/ 

     @Override 
     protected String doInBackground(String... params) { 


      String idd = getIntent().getExtras().getString("idDoc"); 

        int success; 
        try { 

         List<NameValuePair> param = new ArrayList<NameValuePair>(); 
         param.add(new BasicNameValuePair("id",idd)); 

         JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "GET", param); 

         Log.d("Doctor Profile", json.toString()); 

         success = json.getInt(TAG_SUCCESS); 

         if (success == 1) { 

          JSONArray docProfile = json.getJSONArray(TAG_DOCTOR); 

          JSONObject docProf = docProfile.getJSONObject(0); 

          nameDoc = (TextView) findViewById(R.id.name); 
          iddoc = (TextView) findViewById(R.id.docId); 
          icdoc = (TextView) findViewById(R.id.icdoctor); 
          adddoc = (TextView) findViewById(R.id.addressdoc); 
          notel = (TextView) findViewById(R.id.noteldoc); 

          nameDoc.setText(docProf.getString("name")); 
          iddoc.setText(docProf.getString("id")); 
          icdoc.setText(docProf.getString("ic")); 
          adddoc.setText(docProf.getString("address")); 
          notel.setText(docProf.getString("noTel")); 

         } else { 
          //no doctor 
         } 

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




      return null; 
     } 

     protected void onPostExecute(String file_url) { 
      // dismiss the dialog once product deleted 
      pDialog.dismiss(); 
      //if (file_url != null){ 
       // Toast.makeText(docProfile.this, file_url, Toast.LENGTH_LONG).show(); 
      // } 

     } 


    } 
} 
+1

你读过一些http://stackoverflow.com/search?q=CalledFromWrongThreadException的?如果没有,我会建议这[评论](http://stackoverflow.com/questions/10118301/android-viewrootimplcalledfromwrongthreadexception#comment34514715_10118377) – 2015-04-28 13:55:46

+0

是的,不要从错误的线程调用。 https://stackoverflow.com/q/5161951/603270 – shkschneider

+0

是的,我已经尝试了方法..和应用程序崩溃。 – harry

回答

0

该错误清楚地告诉您,您无法在主线程之外访问您的View层次结构。

产生的原因:android.view.ViewRootImpl $ CalledFromWrongThreadException: 只有创建视图层次可以触摸它 享有原来的线程。

由于您使用的是AsyncTask,因此您最好更新onPostExecute中的UI元素。此方法将在主线程上运行。您可以将docProf传入onPostExecute并在此处进行更改。

protected void onPostExecute(String file_url) { 
    nameDoc = (TextView) findViewById(R.id.name); 
    iddoc = (TextView) findViewById(R.id.docId); 
    icdoc = (TextView) findViewById(R.id.icdoctor); 
    adddoc = (TextView) findViewById(R.id.addressdoc); 
    notel = (TextView) findViewById(R.id.noteldoc); 

    nameDoc.setText(docProf.getString("name")); 
    iddoc.setText(docProf.getString("id")); 
    icdoc.setText(docProf.getString("ic")); 
    adddoc.setText(docProf.getString("address")); 
    notel.setText(docProf.getString("noTel")); 

    pDialog.dismiss(); 
} 

将数据发送到onPostExecute,你需要实现三个转变:

1:

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

变为...

class DocProfile extends AsyncTask<String, String, JSONObject> { 

2.

if (success == 1) { 

变为...

if (success == 1) return docProf; 

3.

protected void onPostExecute(String file_url) { 

变为...

protected void onPostExecute(JSONObject docProf) { 

您现在可以在onPostExecute使用docProf。

或者,一切都在此块:

if (success == 1) { 

...需要在UI线程上运行。

例子:

(new Handler(Looper.getMainLooper()).post(new Runnable() { 
    public void run() { 
     nameDoc = (TextView) findViewById(R.id.name); 
     iddoc = (TextView) findViewById(R.id.docId); 
     icdoc = (TextView) findViewById(R.id.icdoctor); 
     adddoc = (TextView) findViewById(R.id.addressdoc); 
     notel = (TextView) findViewById(R.id.noteldoc); 

     nameDoc.setText(docProf.getString("name")); 
     iddoc.setText(docProf.getString("id")); 
     icdoc.setText(docProf.getString("ic")); 
     adddoc.setText(docProf.getString("address")); 
     notel.setText(docProf.getString("noTel")); 
    } 
}); 
+0

如何将docProf传递给onPostExecute? – harry

+0

对不起..我还是新的.. – harry

+0

查看我的更新回答 – Knossos

相关问题