有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何检查正在执行的类

我想检查类是否正在执行。因为它没有给出inputName和inputEmail的任何值。从数据库中检索该值

这是代码的一部分(SingleSubject.java)

public class SingleSubject extends Activity {

EditText txtName;
EditText txtEmail;

String matrix_id;

// Progress Dialog
private ProgressDialog pDialog;

// JSON parser class
JSONParser jsonParser = new JSONParser();

// single product url
private static final String url_subject_details = "http://192.168.1.12/安卓_project/get_subject_details.php";

// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_STUDENT = "student";

private static final String TAG_MATRIX_ID = "matrix_id";
private static final String TAG_NAME = "name";
private static final String TAG_EMAIL = "email";

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


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

    // getting matrix_id from intent
    matrix_id = i.getStringExtra(TAG_MATRIX_ID);
    TextView test = (TextView) findViewById(R.id.matrix_id);
    test.setText(matrix_id);

    // Getting complete matrix_id details in background thread
    new GetSubjectDetails().execute();


}

/**
 * Background Async Task to Get complete matrix_id details
 * */
class GetSubjectDetails extends AsyncTask<String, String, String> {

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


    /**
     * Getting subject details in background thread
     * */
    protected JSONObject doInBackground(String... args) {

        JSONObject json = null;

        try {
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("matrix_id", matrix_id));
                json = jsonParser.makeHttpRequest(url_subject_details, "GET", params);

            }

        catch(Exception e)
            {
              e.printStackTrace();
            }   
        return json;

    }


    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(JSONObject json) {
        super.onPostExecute(json);

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

        // json success tag
        success = json.getInt(TAG_SUCCESS);

        if (success.equals(1)) {
            // successfully received subject details
            JSONArray subjectObj = json.getJSONArray(TAG_STUDENT); // JSON Array

            // get first subject object from JSON Array
            JSONObject subject = subjectObj.getJSONObject(0);

        // subject with this matrix_id found
        // Edit Text
        txtName = (EditText) findViewById(R.id.inputName);
        txtEmail = (EditText) findViewById(R.id.inputEmail);

        // display subject data in EditText
        txtName.setText(subject.getString(TAG_NAME));
        txtEmail.setText(subject.getString(TAG_EMAIL));         

        }
        // dismiss the dialog once got all details      
        pDialog.dismiss();


    }
}

  }

共 (1) 个答案

  1. # 1 楼答案

    doInbackground在后台线程上调用。不需要在那里创建线程

    txtName = (EditText) findViewById(R.id.inputName);
    txtEmail = (EditText) findViewById(R.id.inputEmail);
    txtName.setText(subject.getString(TAG_NAME)); // updating ui not possible in doInbackground
    txtEmail.setText(subject.getString(TAG_EMAIL));
    

    其次,不能从后台线程更新ui。您需要在onPostExecute中更新ui。您可以在doInbackground中返回结果,它是onPostExecute的参数,并在那里更新ui

    你需要看看AsyncTask的文档来了解更多信息

    http://developer.android.com/reference/android/os/AsyncTask.html

    编辑:

    改变这个

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

     class GetSubjectDetails extends AsyncTask<String, String, JSONOBject> {
    

    然后

     protected JSONObject doInBackground(String... params) {
    
                JSONObject json =null;            
                try {
                    List<NameValuePair> params = new ArrayList<NameValuePair>();
                    params.add(new BasicNameValuePair("matrix_id", matrix_id));
                    json = jsonParser.makeHttpRequest(
                            url_subject_details, "GET", params);
                    }catch(Exception e)
                    {
                      e.printStacktrace();
                    }   
        return json;
      }
    

    然后

     @Override
     protected void onPostExecute(JSONObject json) {
        super.onPostExecute(json);
        pDialog.dismiss();
        // parse json
        // update ui  
    }