2013-10-25 46 views
0

一切都很好,直到我用doInBackground(Object... arg0)才显示ProgressDialog,直到它被加载然后在onPostExecute(Object result)方法中解除它。 LogCat中没有错误消息,只是崩溃。请帮忙?我使用doInBackground(Object ... arg0)后,我的应用程序崩溃了

Vacancy.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); 
} 

}

+0

有没有这样的事情,“只是崩溃”。你正在从doinbg访问ui,'lisView1.setAdapter(sAdap);'。这是不允许的。 – njzk2

回答

3

您是从哪个doInbackground你不应该做更新UI。返回doInbackground中的结果并更新onPostExecute中的ui。

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中初始化适配器并将适配器设置为listview。

@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

您还需要移动内部下方onCreatesetContentView后堂妹findViewById查找当前膨胀的布局的视图。

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); 
+0

检查我更新的代码。但现在它给了我两个错误:第一,它说'不能返回MyArrList',第二它说'从onPostExecute(ArrayList > result)移除@Override' – EthioDroid

+0

这个建议是正确的。 @Override是注释 – Raghunandan

+0

@EthioDroid在这里检查http://developer.android.com/reference/android/os/AsyncTask.html – Raghunandan