TextView的值與空指針異常 崩潰下面是我使用的代碼。代碼中有任何問題。在上的AsyncTask的onPostExecute TextView的值沒有設置
public class MainActivity extends Activity {
Button btn;
int i =0;
SharedPreferences.Editor preferences;
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.button1);
preferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.this).edit();
txt = (TextView) findViewById(R.id.textView1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new LongOperation().execute("");
}
});
}
private class LongOperation extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.interrupted();
}
}
return "Executed";
}
@Override
protected void onPostExecute(String result) {
txt.setText("Executed ----------------------- ");
}
@Override
protected void onPreExecute() {}
@Override
protected void onProgressUpdate(Void... values) {}
}
}
它即將onPostExecute TextView的值不是設定值後。 請幫幫我。提前致謝。
發佈您的xml代碼activity_main。 – Drv
http://stackoverflow.com/questions/11680631/asynctask-onpostexecute-not-updating-textview試試吧...... – BPatel
你是否得到任何異常? – Exception