次のコードでは、強制終了エラーが発生します
public class MainActivity extends Activity {
Button b1;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button1);
tv = (TextView) findViewById(R.id.textView1);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new loadSomeStuff().execute();
}
});
}
public class loadSomeStuff extends AsyncTask<String, Integer, String> {
ProgressDialog dlg;
protected void onPreExecute() {
dlg = new ProgressDialog(MainActivity.this);
dlg.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dlg.setMessage("Loding Please Wait");
dlg.setMax(100);
dlg.show();
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
for (int i = 0; i < 20; i++) {
try {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(
Environment.getExternalStorageDirectory() + "/"
+ "/bg.pdf"));
document.open();
document.add(new Paragraph("hie"));
Toast.makeText(getApplicationContext(), "Generated",
Toast.LENGTH_LONG).show();
document.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
publishProgress(5);
try {
Thread.sleep(88);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
dlg.dismiss();
return null;
}
protected void onProgressUpdated(Integer... progress) {
dlg.incrementProgressBy(progress[0]);
}
protected void onPostExecute(String result) {
tv.setText("DONE");
}
}