0

一切都很好,直到我使用它doInBackground(Object... arg0)来显示ProgressDialog,直到它被加载然后在onPostExecute(Object result)方法上关闭它。a 中没有错误消息LogCat,只是崩溃。请帮忙?

空缺.java

 package com.apps.vacancy;

//all the necessary imports are imported

public class Vacancy extends Activity {

public static String urlPageNumHolder;
public ProgressDialog dialog;

ListView lisView1;
EditText inputText;

@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.vacancy);

    lisView1 = (ListView) findViewById(R.id.listView1);
    inputText = (EditText) findViewById(R.id.editText1);

    // Permission StrictMode
    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }

    final Button btn1 = (Button) findViewById(R.id.button1);
    btn1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            searchJob();
        }
    });

}

public void searchJob() {
    new LoadContentFromServer().execute();
    dialog = ProgressDialog.show(this, "Vacancy", "Loading...", true, false);
}

public String getJSONUrl(String url, List<NameValuePair> params) {
    StringBuilder str = new StringBuilder();
    HttpClient client = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(url);

    try {
        httpPost.setEntity(new UrlEncodedFormEntity(params));
        HttpResponse response = client.execute(httpPost);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) { // Download OK
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) {
                str.append(line);
            }
        } else {
            Log.e("Log", "Failed to download file..");
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return str.toString();
}

class LoadContentFromServer extends AsyncTask<Object, Integer, Object> {

    @Override
    protected ArrayList<HashMap<String, String>> doInBackground(Object... arg0) {

        String url = "http://10.0.2.2/android/smartaddis/mobile/vacancy/getVacancy.php";

        // Paste Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("txtKeyword", inputText.getText().toString()));

        try {
            JSONArray data = new JSONArray(getJSONUrl(url, params));

            final ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
            HashMap<String, String> map;

            for (int i = 0; i < data.length(); i++) {
                JSONObject c = data.getJSONObject(i);

                map = new HashMap<String, String>();
                map.put("id", c.getString("id"));
                map.put("title_en", c.getString("title_en"));
                map.put("description_en", c.getString("description_en"));
                map.put("posteddate", c.getString("posteddate"));
                map.put("expiredate", c.getString("expiredate"));
                MyArrList.add(map);

            }

            lisView1.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> myAdapter, View myView,
                        int position, long mylng) {

                    urlPageNumHolder = MyArrList.get(position).get("id").toString();

                    Intent subActivity = new Intent(Vacancy.this,
                            VacancyWebber.class);

                    Bundle translateBundle = ActivityOptions
                            .makeCustomAnimation(Vacancy.this,
                                    R.anim.slide_in_left, R.anim.slide_out_left).toBundle();
                    startActivity(subActivity, translateBundle);
                }
            });

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

    @Override
    protected void onPostExecute (ArrayList<HashMap<String, String>> result) {
        if (dialog != null)
            dialog.dismiss();

        SimpleAdapter sAdap = new SimpleAdapter(Vacancy.this, result,
                R.layout.vacancy_column, new String[] { "title_en",
                        "description_en", "posteddate", "expiredate" }, new int[] { R.id.jobtitle,
                        R.id.jobdescription, R.id.jobdateTime, R.id.jobexpiredate });

        lisView1.setAdapter(sAdap);
    }

}

@Override
public void finish() {
    super.finish();
    overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_right);
}

}

4

1 回答 1

3

您正在更新doInbackground不应执行的 ui。返回结果doInbackground并更新 ui 中onPostExecute

作为doInbackground回报MyArrList。后台计算的结果是传递给onPostExecute.

@Override
protected ArrayList<HashMap<String, String>> doInBackground(Object... params) {
    // TODO Auto-generated method stub
    ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
            ... // rest of the code
    return MyArrList;
}

onPostExecute初始化适配器并将适配器设置为列表视图。

@Override
protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
    // TODO Auto-generated method stub
    super.onPostExecute(result);
            //  dimiss dialog
            SimpleAdapter sAdap = new SimpleAdapter(Vacancy.this, result,
                    R.layout.vacancy_column, new String[] { "title_en",
                            "description_en", "posteddate", "expiredate" }, new int[] { R.id.jobtitle,
                            R.id.jobdescription, R.id.jobdateTime, R.id.jobexpiredate });

            lisView1.setAdapter(sAdap);
            ... // rest of the code
} 

欲了解更多信息

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

在coz在当前的膨胀布局中查找视图onCreate之后,您还需要将下面的内容移动到内部。setContentViewfindViewById

ListView lisView1;
EditText inputText;
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.vacancy);
lisView1 = (ListView) findViewById(R.id.listView1);
inputText = (EditText) findViewById(R.id.editText1);
于 2013-10-25T15:26:22.997 回答