Android 我的应用程序在使用doInBackground(对象…arg0)后崩溃
在我使用Android 我的应用程序在使用doInBackground(对象…arg0)后崩溃,android,Android,在我使用doInBackground(Object…arg0)以显示ProgressDialog之前,一切都很好,直到它被加载,然后在onPostExecute(Object result)方法上解除它。日志猫中没有错误消息,只是崩溃了。请帮忙 java package com.apps.vacancy; //all the necessary imports are imported public class Vacancy extends Activity { public stat
doInBackground(Object…arg0)
以显示ProgressDialog
之前,一切都很好,直到它被加载,然后在onPostExecute(Object result)
方法上解除它。日志猫中没有错误消息,只是崩溃了。请帮忙
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);
}
package.com.apps.空缺;
//所有必要的进口品都是进口的
公开课空缺扩大活动范围{
公共静态字符串urlPageNumHolder;
公共对话;
ListView lisView1;
编辑文本输入文本;
@SuppressLint(“新API”)
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局,空白);
lisView1=(ListView)findViewById(R.id.listView1);
inputText=(EditText)findViewById(R.id.editText1);
//权限限制模式
如果(android.os.Build.VERSION.SDK_INT>9){
StrictMode.ThreadPolicy policy=新建StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(策略);
}
最终按钮btn1=(按钮)findViewById(R.id.button1);
btn1.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
searchJob();
}
});
}
公共无效搜索作业(){
新建LoadContentFromServer().execute();
dialog=ProgressDialog.show(这是“空缺”,“加载…”,true,false);
}
公共字符串getJSONUrl(字符串url,列表参数){
StringBuilder str=新的StringBuilder();
HttpClient=new DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
试一试{
setEntity(新的UrlEncodedFormEntity(参数));
HttpResponse response=client.execute(httpPost);
StatusLine StatusLine=response.getStatusLine();
int statusCode=statusLine.getStatusCode();
如果(statusCode==200){//下载正常
HttpEntity=response.getEntity();
InputStream内容=entity.getContent();
BufferedReader reader=新的BufferedReader(
新的InputStreamReader(内容));
弦线;
而((line=reader.readLine())!=null){
str.append(行);
}
}否则{
Log.e(“Log”,“未能下载文件…”);
}
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
return str.toString();
}
类LoadContentFromServer扩展了AsyncTask{
@凌驾
受保护的ArrayList doInBackground(对象…arg0){
字符串url=”http://10.0.2.2/android/smartaddis/mobile/vacancy/getVacancy.php";
//粘贴参数
List params=new ArrayList();
add(新的BasicNameValuePair(“txtKeyword”,inputText.getText().toString());
试一试{
JSONArray数据=新的JSONArray(getJSONUrl(url,params));
final ArrayList MyArrList=新的ArrayList();
HashMap图;
对于(int i=0;i
}您正在从
doInbackground
更新ui,您不应该这样做。在doInbackground
中返回结果,并在onPostExecute
中更新ui
@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;
}
在doInbackground
returnMyArrList
中。后台计算的结果被传递到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;
}
更多信息
您还需要在setContentView
cozfindViewById
在当前扩展的布局中查找视图后,将下面的onCreate
移到内部
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);
您正在从
doInbackground
更新ui,您不应该这样做。在doInbackground
中返回结果,并在onPostExecute
中更新ui
@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;
}
在doInbackground
returnMyArrList
中。后台计算的结果被传递到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;
}
更多信息
您还需要将下面的内容移动到onCreate中