Android AsyncTast onPostExecute不更新UI元素:引发NullPointerException
我一度陷入这个问题。 这是我的密码Android AsyncTast onPostExecute不更新UI元素:引发NullPointerException,android,android-asynctask,Android,Android Asynctask,我一度陷入这个问题。 这是我的密码 public class PilgrimFriendActivity extends Activity { private String DOMAIN = "http://"; private String LAWS_URL = ""; private String LOCATIONS_URL = ""; private ListView list; private TextView content; pr
public class PilgrimFriendActivity extends Activity {
private String DOMAIN = "http://";
private String LAWS_URL = "";
private String LOCATIONS_URL = "";
private ListView list;
private TextView content;
private Law law ;
private Location location;
private ProgressDialog dialog;
private SharedPreferences preferences;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
preferences = getSharedPreferences("com.hackontech.pilgrim_preferences", MODE_PRIVATE);
this.DOMAIN += preferences.getString("DOMAIN","192.168.0.104");
this.LAWS_URL = DOMAIN + "/pilgrimfriend/main/getAllLaws";
this.LOCATIONS_URL = DOMAIN + "/pilgrimfriend/main/getAllLocations";
dialog = new ProgressDialog(this);
content = (TextView) findViewById(R.id.content);
list = (ListView) findViewById(R.id.laws);
this.lLaws();
}
public void lLaws() {
final class RequestTask extends AsyncTask<String, String, String>{
private String URL = null;
@Override
protected String doInBackground(String... uri) {
String result = null;
URL = uri[0];
try {
//Create default http client
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(URL);
Log.v("URL",URL);
HttpResponse response = httpclient.execute(httpget);
//Retrieve the response body or "entity"
HttpEntity entity = response.getEntity();
//Return the entity as String
result = EntityUtils.toString(entity);
Log.v("result",result.toString());
} catch (Exception e) {
return e.getMessage();
}
return result;
}
@Override
protected void onPostExecute(String result) {
//super.onPostExecute(result);
dialog.dismiss();
try{
JSONArray jArray = new JSONArray(result);
JSONObject json_data;
ArrayList<String> items = new ArrayList<String>();
for(int i=0; i < jArray.length() ; i++) {
json_data = jArray.getJSONObject(i);
int id=json_data.getInt("id");
String title=json_data.getString("title");
String scholor =json_data.getString("scholor");
items.add(title+" ("+scholor+")");
}
content.setText(result);
ArrayAdapter<String> mArrayAdapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1, items);
list.setAdapter(mArrayAdapter);
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
catch(JSONException e) {
//content.setText(e.getMessage());
}
//Do anything with response..
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dialog.setTitle("Downloading Laws Data");
dialog.show();
}
}
new RequestTask().execute(LAWS_URL);
}
由于某种原因,
onPostExecute
方法在方法完成后立即执行所有UI更新
例如:
@Override
protected Void doInBackground() {
someTextView.setText("Do In Background Finished!");
}
@Override
protected void onPostExecute () {
someTextView.setText("Post Execute Started");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// ..
}
someTextView.setText("Post Execute Finished");
}
将导致显示“后台执行完成!”并在5秒后(当
onPostExecute
结束时)将用“Post Execute Started”更新文本,并立即用“Post Execute Finished”再次更新文本。我猜内容
为空。你初始化它了吗?是的,我可以在其他任何地方更新它,不仅内容,listview也不工作,简言之,它没有更新UI,但奇怪的是进程对话框正在显示,Toast确实显示添加崩溃日志
和行号<代码>对话框显示还是取消?对话框取消,让我发布完成code@user868900日志中的异常回溯告诉您引发异常的确切行号,为什么不简单地检查那里发生了什么?在那里放置一个断点并在调试器控制下运行应用程序怎么样?这些都是基本的东西。
@Override
protected Void doInBackground() {
someTextView.setText("Do In Background Finished!");
}
@Override
protected void onPostExecute () {
someTextView.setText("Post Execute Started");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// ..
}
someTextView.setText("Post Execute Finished");
}